Count the values inside a cell array considering another cell array
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2019-10-16
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.
采纳的回答
Stephen23
2019-10-16
编辑:Stephen23
2019-10-16
>> 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 个评论
Stephen23
2019-10-16
>> 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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!