Count the values inside a cell array considering another cell array

1 view (last 30 days)
luca on 16 Oct 2019
Commented: luca on 16 Oct 2019
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?
luca on 16 Oct 2019
Thanks for the comment!
May you help me Stephen?
I was thinking about substitute the first row of each cell of F with the second row of V. Then again invert the row in F and then reapeat the code you suggest me in the previous question, with sumabs instead of sum.
But do you know how to implement/substitute the rows?

Stephen on 16 Oct 2019
Edited: Stephen 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
luca on 16 Oct 2019
THANKS A LOT

R2019b

Community Treasure Hunt

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

Start Hunting!