Count occurances on multidimensional matrix with multiple criteia on different dimnesions

1 回表示 (過去 30 日間)
Daniel
Daniel 2022 年 10 月 5 日
コメント済み: Daniel 2022 年 10 月 5 日
I would like to count occurances of >= 90 on page 1 and = 2 on page 2 of a multidimensional matrix where both criteria in the same location need to be met to be true for the following:
A(:,:,1)=
[90 95 90 80
70 90 95 70
60 90 90 60]
A(:,:,2)=
[1 1 1
2 2 2
1 2 1]
The answer for the above example should be 3.
Thank you in advance for any assistance.

回答 (2 件)

Torsten
Torsten 2022 年 10 月 5 日
編集済み: Torsten 2022 年 10 月 5 日
A = zeros(3,4,2);
A(:,:,1)=...
[90 95 90 80
70 90 95 70
60 90 90 60];
A(:,:,2)=...
[1 1 1 1
2 2 2 2
1 2 1 1];
occurences = nnz(A(:,:,1)>=90 & A(:,:,2)==2)
occurences = 3
Don't know why A(:,:,1) is 3x4 and A(:,:,2) is 3x3 though.

Davide Masiello
Davide Masiello 2022 年 10 月 5 日
A(:,:,1) = [ 90 95 90 80;...
70 90 95 70;...
60 90 90 60;...
];
A(:,:,2) = [ 1 1 1 1;...
2 2 2 2;...
1 2 1 1;...
];
B = (A(:,:,1) >= 90) + (A(:,:,2) == 2)
B = 3×4
1 1 1 0 1 2 2 1 0 2 1 0
N = sum(B==2,'all')
N = 3

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by