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