Count how many element are inside each cell af a cell array on the basis of an array

12 views (last 30 days)
Hi given a cell array
V={{[1,1,1,1;25,45,70,90],[2,2,2,2;78,112,146,180],[3,3,3,3;93,127,161,195],[4,4;70,100],[6;85],[9,9;85,110]},{[],[2,2,2,2;73,107,141,175],[3,3,3,3;83,117,151,185],[4,4,4,4;65,85,105,125],[6;85],[9,9,9;80,105,130]}};
and an array
SP= [1 2 3 4 6 9]
I want to create a matrix M that has as many rows as the cell in V (in this case 2)
CONSIDERING THE FIRST CELL:
Inside the row of M how want to count how many elements are present in each cell of the same type, and that cannot exceed a value of 80 (<80) referring to the second raw of each cell.
considering element 1 in V{1,1} that occupies V{1,1}{1,1}
In this case the number of 1 that do not exceed 80 is 3.
take the element 2
is just 1.
Doing the same for all the elements and cells i ontain M:
M= [3 1 0 1 0 0; 0 1 0 1 0 1]
May someone help me with the code?
  6 Comments
luca
luca on 16 Oct 2019
Edited: luca on 16 Oct 2019
SP is the input of my algorithm. I want to show you just a part of the code through which I have obtained V.
clear all;
clc
SP= [1 2 3 4 6 9];
M = [0 1 1 1 0 1 1 1 1];
T= [20 34 34 20 34 20 25 34 25];
GGG = {[1 2 2 1 3 4 9 9 6 1 3 3 2 1 2 4 3 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85],[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78]};
for i=1:numel(GGG)
G=GGG{i};
SP=unique(G(1,:));
for j=1:length(SP)
[~,indx]=find(G(1,:)==SP(j));
R{1,i}{1,SP(j)}=[SP(j); M(SP(j))*T(SP(j))];
V{1,i}{1,SP(j)}=[SP(j); M(SP(j))*T(SP(j))+G(2,indx(1))+T(SP(j))];
if length(indx)>=2
for k=2:length(indx)
R{1,i}{1,SP(j)}=[R{1,i}{1,SP(j)}, [SP(j); G(2,indx(k))-V{1,i}{1,SP(j)}(2,(k-1))]];
if R{1,i}{1,SP(j)}(2,end)<0
V{1,i}{1,SP(j)}=[V{1,i}{1,SP(j)}, [SP(j); G(2,indx(k))+T(SP(j))-R{1,i}{1,SP(j)}(2,end)]];
else
V{1,i}{1,SP(j)}=[V{1,i}{1,SP(j)}, [SP(j); G(2,indx(k))+T(SP(j))]];
end
end
end
end
end
SP= [1 2 3 4 6 9];
V = cellfun(@(x) x(:,SP),V,'uni',0);
As you can see I need SP every time in order to track which attributes I'm creating through the sequence. That is, 9 for example will always be in the 6th column. At the next run I want to substitute and use a new SP, for example [1 3 4 5 7 9]. The idea is that I will change every time and run the code with the values in SP, according to which attributes I want to produce.
Now, if the problem is
VM = cellfun(@(z)cellfun(@(x)sum(x(2,:)<= 80),z),V,'UniformOutput',false);
and I cannot found the second raw in the second cell. If we are able to insert just zeros in that cell that is empty, probably we solve the problem. So maybe in the code I post to you, is it possible to modify and obtain zeros? maybe modifying
V = cellfun(@(x) x(:,SP),V,'uni',0);
Or is there a smarter solution?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Oct 2019
Edited: Stephen23 on 16 Oct 2019
>> C = vertcat(V{:});
>> X = ~cellfun('isempty',C);
>> F = @(x)sum(x(2,:)<=80);
>> M = zeros(size(C));
>> M(X) = cellfun(F,C(X))
M =
3 1 0 1 0 0
0 1 0 1 0 1
Nothing in your question or comments seems to require SP, so I have ignored it.

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!