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

조회 수: 23 (최근 30일)
elroi berkovits
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
Stephen23
Stephen23 2020년 1월 9일
편집: Stephen23 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
elroi berkovits 2020년 1월 9일
Thanks so much, it's great!

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2020년 1월 9일
편집: 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개)

Meg Noah
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
Meg Noah
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
Meg Noah 2020년 1월 9일
This works too.
idxAllGreaterThan50 = find(sum(imbinarize(myArray,50),2)==size(myArray,2));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by