Count the values inside a cell array considering another cell array
5 views (last 30 days)
Show older comments
Hi given a cell
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
C = vertcat(V{:});
X = ~cellfun('isempty',C);
F = @(x)sum(x(2,:)<=80);
F give us the value of which in each cell of V we exceed the value 80.
knowing this, I would like to consider R
R={{[1,1,1,1;0,-5,5,0],[2,2,2,2;34,-63,-47,-71],[3,3,3,3;34,-38,-67,-76],[4,4;20,10],[6;20],[9,9;25,-45]},{[],[2,2,2,2;34,-63,-37,-66],[3,3,3,3;34,-63,-57,-86],[4,4,4,4;20,-30,-35,-27],[6;20],[9,9,9;25,-40,-50]}};
and sum the absolute values of the second raw of each cell till the column where I've exceed 80 in V is met.
NOTE THAT V AND R have the same cell dimensions. In fact the first row of each cell in V and R are the same.
The result shoul be a matrix Y (2*6) that collect the absolute value sum of the second raw of each cell. 2 beacuse we have two cell in R, and 6 because in each cell there are 6 cell again.
Could someone help me?
2 Comments
Stephen23
on 16 Oct 2019
Original question:
"F give us the value of which in each cell of V we exceed the value 80."
Actually the anonymous function F does not know anything about V, nor does it have anything to do with cell arrays. Also, you calculate X but do not use it anywhere.
Without the context of my answer those three lines are not very informative. It would be much better to just paste a link to your earlier question or to my answer.
Accepted Answer
Stephen23
on 16 Oct 2019
Edited: Stephen23
on 16 Oct 2019
>> Rc = vertcat(R{:});
>> Vc = vertcat(V{:});
>> X = ~cellfun('isempty',Rc) & ~cellfun('isempty',Vc);
>> F = @(r,v) sum(r(2,v(2,:)<=80)); % without ABS
>> M = zeros(size(Rc));
>> M(X) = cellfun(F,Rc(X),Vc(X))
M =
0 34 0 20 0 0
0 34 0 20 0 25
Or if you really want to sum the absolute values:
>> F = @(r,v) sum(abs(r(2,v(2,:)<=80))); % with ABS
>> M = zeros(size(Rc));
>> M(X) = cellfun(F,Rc(X),Vc(X))
M =
10 34 0 20 0 0
0 34 0 20 0 25
6 Comments
Stephen23
on 16 Oct 2019
>> C = vertcat(R{:});
>> X = ~cellfun('isempty',C);
>> F = @(x)sum(max(0,x(2,:)));
>> M = zeros(size(C));
>> M(X) = cellfun(F,C(X))
M =
5 34 34 30 20 25
0 34 34 20 20 25
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!