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

Stephen23
Stephen23 2017년 10월 25일
편집: Stephen23 2017년 10월 25일
"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
KSSV 2017년 10월 25일

0 개 추천

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
Andrei Bobrov 2017년 10월 25일

0 개 추천

M = cat(3,A,B,C);
out = squeeze(sum(M >= 3,2));

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 10월 25일

답변:

2017년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by