MATLAB Answers

0

I need a code to count how many raw in my matrix above zero?

Asked by Abdullah Alsuhaymi on 17 Nov 2019 at 3:53
Latest activity Commented on by Abdullah Alsuhaymi on 17 Nov 2019 at 19:34
I have matrix and I need a code to count how many row contain negative integer
B =
1 -1
2 -2
3 3

  0 Comments

Sign in to comment.

2 Answers

Answer by Image Analyst
on 17 Nov 2019 at 17:56
 Accepted Answer

Try using sum() and any():
B =[
1 -1
2 -2
3 3]
numberOfNegativeRows = sum(any(B < 0, 2))

  1 Comment

Thank you so much it works

Sign in to comment.


Answer by Erivelton Gualter on 17 Nov 2019 at 3:57

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 Comments

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).
yes , this count the total number of nagative numbers in the matrix but I am looking for number of nagative rows ?
can you help me please ?

Sign in to comment.