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

23 次查看(过去 30 天)
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

采纳的回答

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