Summing up counts in multiple matrices
显示 更早的评论
Hi all, complete newbie to matlab!
I have 15 matrices each with 2 rows and 100 columns. I need to count the number of values equal to or greater than 3 in each column for each row in each matrix (i.e. how many of the 100 columns contain values greater than or equal to 3) and then combine those counts in a single matrix for all 15.
A more manageable example:
If I have 3 matrices:
A = [2 3 9 6 5; 5 4 8 10 11]
B = [8 9 5 6 7; 2 1 5 9 4]
C = [ 1 3 2 6 8; 3 1 4 5 7]
How many columns have values equals or greater than 3?
A = [5;5] B = [5;4] and C = [3;4]
Final matrix = [5,5,3; 5,4,4]
Thanks!
1 个评论
"I have 15 matrices ..."
And that is the start of making this problem difficult to solve. If you had simply stored yoru data in one ND array or one cell array, then your task could be solved trivially using simple vectorized code or a loop.
By deciding to have lots of separate variables, you make your code more complex, and solving this problem much harder.
回答(2 个)
KSSV
2017-10-25
A = [2 3 9 6 5; 5 4 8 10 11]
B = [8 9 5 6 7; 2 1 5 9 4]
C = [ 1 3 2 6 8; 3 1 4 5 7]
A = sum(A>=3,2) ;
B = sum(B>=3,2) ;
C = sum(C>=3,2) ;
[A B C]
Andrei Bobrov
2017-10-25
M = cat(3,A,B,C);
out = squeeze(sum(M >= 3,2));
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!