Asked by Abdullah Alsuhaymi
on 17 Nov 2019 at 3:53

I have matrix and I need a code to count how many row contain negative integer

B =

1 -1

2 -2

3 3

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))

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))

Image Analyst
on 17 Nov 2019 at 17:55

Abdullah Alsuhaymi
on 17 Nov 2019 at 19:31

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.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.