Summing up counts in multiple matrices
Mostrar comentarios más antiguos
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 comentario
"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.
Respuestas (2)
KSSV
el 25 de Oct. de 2017
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
el 25 de Oct. de 2017
M = cat(3,A,B,C);
out = squeeze(sum(M >= 3,2));
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!