Count the values inside a cell array considering another cell array

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 Commenti

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.
luca
luca il 16 Ott 2019
Modificato: luca il 16 Ott 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?

Accedi per commentare.

 Risposta accettata

>> 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 Commenti

luca
luca il 16 Ott 2019
Modificato: luca il 16 Ott 2019
May I ask just one more thing? In case I want to sum just the values in the second row of F that are positive (>0).
How could I modify the code?
Rc = cellfun(@(v) v(v >= 0), Rc, 'UniformOutput', false);
It could be a solution, but it's not a good solution for 2 things:
1) I should also substitute with zeros the negative values
2) cause it's better to leave the negative and just sum the positive later
But I have no idea of how to do it. I'm not familiar with cellfun
"In case I want to sum just the values in the second row of F that are positive (>0)"
In your original question you wanted to sum the second row of the matrices of R until "the column where I've exceed 80 in V is met". Now you have given a new condition, that you only want to sum the positive values.
Does your new condition completely replace the original condition (which I already showed you how to do), or is it in addition to the original condition (i.e. you want both conditions to be met)?
luca
luca il 16 Ott 2019
Modificato: luca il 16 Ott 2019
Just completely replace. So NO addition to the previous
Thanks for ask this !
>> 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

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2019b

Tag

Richiesto:

il 16 Ott 2019

Commentato:

il 16 Ott 2019

Community Treasure Hunt

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

Start Hunting!

Translated by