I need a code to count how many raw in my matrix above zero?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abdullah Alsuhaymi
le 17 Nov 2019
Commenté : Abdullah Alsuhaymi
le 17 Nov 2019
I have matrix and I need a code to count how many row contain negative integer
B =
1 -1
2 -2
3 3
0 commentaires
Réponse acceptée
Image Analyst
le 17 Nov 2019
Try using sum() and any():
B =[
1 -1
2 -2
3 3]
numberOfNegativeRows = sum(any(B < 0, 2))
Plus de réponses (1)
Erivelton Gualter
le 17 Nov 2019
You might use the function find (https://www.mathworks.com/help/matlab/ref/find.html).
Check the following code:
B = [1,-1; 2,-2; 3,3];
length(find(B<0))
2 commentaires
Image Analyst
le 17 Nov 2019
This counts the total number of negative numbers, not the number of rows. You might think what could happen if there are two negative numbers in one or more of the rows (answer in my Answer).
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices 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!