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

1 visualización (últimos 30 días)
I have matrix and I need a code to count how many row contain negative integer
B =
1 -1
2 -2
3 3

Respuesta aceptada

Image Analyst
Image Analyst el 17 de Nov. de 2019
Try using sum() and any():
B =[
1 -1
2 -2
3 3]
numberOfNegativeRows = sum(any(B < 0, 2))

Más respuestas (1)

Erivelton Gualter
Erivelton Gualter el 17 de Nov. de 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 comentarios
Image Analyst
Image Analyst el 17 de Nov. de 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).
Abdullah Alsuhaymi
Abdullah Alsuhaymi el 17 de Nov. de 2019
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 ?

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by