# how to find the sum of rows in an array, that fit a condition?

2 ビュー (過去 30 日間)
elroi berkovits 2020 年 1 月 9 日
コメント済み: elroi berkovits 2020 年 1 月 10 日
i have this array , and i want to find the number of rows that all of the numbers are > = 50
pls help me :-)
30.0047727061869 37.9145153637155 43.1094385378236
62.9706777808096 30.2607950232894 37.0959641999436
54.8026286139397 53.7785240267376 39.9495891072374
63.3652852289308 69.8284216321427 31.3008672624000
43.7698149465365 68.5396733733610 69.8284216321427
45.4522693969714 56.1470861572145 65.7488503116703
33.6770518207058 64.8492448833287 68.6679524542272
30.9547129162769 47.1694311042377 32.2257185243214
69.7530828170370 31.1213028246854 37.7757214644685
30.1831897838518 36.1859674263933 68.3366491886460

#### 2 件のコメント

Stephen Cobeldick 2020 年 1 月 9 日
"...i want to find the number of rows that all of the numbers are > = 50"
None of them:
>> nnz(all(M>=50,2))
ans = 0
elroi berkovits 2020 年 1 月 9 日
Thanks so much, it's great!

サインイン to comment.

### 採用された回答

John D'Errico 2020 年 1 月 9 日

When a problem is too complex to solve, then break it down into SMALLER parts, that you can handle.
You want to know how many rows in your array have the property that all elements are greater than or equal to 50. So, first, can you determine which elements are at least 50? (I hope so.)
A >= 50
What does that do? (Hint: it creates a logical array, that is true when the element is greater than or equal to 50.) A logical array is effectively just an array of 0 or 1 elements.
Next, what does the all function do? (READ THE HELP!) If you want to know if all of the elements in a row are greater than 50, what does this do?
all(A >= 50,2)
What shape is that result? (TRY IT.)
Finally, how many of those rows have the desired property?
sum(all(A >= 50,2))
Break a too big problem down into sub-problems, that hopefully lead in the direction you need to go. When you get stuck, look in MATLAB for tools that will help you to solve the problem you are currently stuck on.

#### 1 件のコメント

elroi berkovits 2020 年 1 月 10 日
TNX

サインイン to comment.

### その他の回答 (1 件)

Meg Noah 2020 年 1 月 9 日
Isn't it obvious since each row has a value that is greater than 50 that the cross sum of terms in the row will be greater than 50? Here's how to find the indices.
myArray = [ ...
30.0047727061869 37.9145153637155 43.1094385378236; ...
62.9706777808096 30.2607950232894 37.0959641999436; ...
54.8026286139397 53.7785240267376 39.9495891072374; ...
63.3652852289308 69.8284216321427 31.3008672624000; ...
43.7698149465365 68.5396733733610 69.8284216321427; ...
45.4522693969714 56.1470861572145 65.7488503116703; ...
33.6770518207058 64.8492448833287 68.6679524542272; ...
30.9547129162769 47.1694311042377 32.2257185243214; ...
69.7530828170370 31.1213028246854 37.7757214644685; ...
30.1831897838518 36.1859674263933 68.3366491886460];
myRowSums = sum(myArray')';
idx = find(myRowSums >= 50);

#### 3 件のコメント

Stephen Cobeldick 2020 年 1 月 9 日
Rather than use conjugate transpose, then the default sum direction, and then conjugate tranpose again on the output, it is more robust and more efficient to specify the dimension:
myRowSums = sum(myArray,2);
Meg Noah 2020 年 1 月 9 日
I think I misread it and she really wants this:
myArray = [ ...
30.0047727061869 37.9145153637155 43.1094385378236; ...
62.9706777808096 30.2607950232894 37.0959641999436; ...
54.8026286139397 53.7785240267376 39.9495891072374; ...
63.3652852289308 69.8284216321427 31.3008672624000; ...
43.7698149465365 68.5396733733610 69.8284216321427; ...
45.4522693969714 56.1470861572145 65.7488503116703; ...
33.6770518207058 64.8492448833287 68.6679524542272; ...
30.9547129162769 47.1694311042377 32.2257185243214; ...
69.7530828170370 31.1213028246854 37.7757214644685; ...
30.1831897838518 36.1859674263933 68.3366491886460];
idxAllGreaterThan50 = find(all(myArray'>50));
Meg Noah 2020 年 1 月 9 日
This works too.
idxAllGreaterThan50 = find(sum(imbinarize(myArray,50),2)==size(myArray,2));

サインイン to comment.

サインイン してこの質問に回答します。

Translated by