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

23 vues (au cours des 30 derniers jours)
elroi berkovits
elroi berkovits le 9 Jan 2020
Commenté : elroi berkovits le 10 Jan 2020
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 commentaires
Stephen23
Stephen23 le 9 Jan 2020
Modifié(e) : Stephen23 le 9 Jan 2020
"...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 le 9 Jan 2020
Thanks so much, it's great!

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 9 Jan 2020
Modifié(e) : John D'Errico le 9 Jan 2020
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.

Plus de réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by