How can I count how many numbers are different from '-1'?
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
Hi,
Example: Suppose we a have a matrix A
A= [1 -1 0 -1 0 0 -1 0 -1 -1 -1 -1 -1 -1 -1]
How can I count how many numbers are different from '-1'?
2 comentarios
  Shubhashree Bal
 el 1 de Nov. de 2019
				Search for how many negative number exists in A. than subtract that from length of array. Or Write a loop for how many positive number exists in array.
Code attached below:
Close all; 
clear all;
A= [1 -1 0 -1 0 0 -1 0 -1 -1 -1 -1 -1 -1 -1];
count = 0;
for i = 1: length(A)
if(A(i)>=0)
count = count +1;
end
end
Respuestas (2)
  Pedro Martinez
 el 1 de Nov. de 2019
        Total = sum (A==-1);
1 comentario
  Walter Roberson
      
      
 el 5 de Nov. de 2019
				That gives the number of rows of -1 but the user wants the number of columns that are not -1
Ver también
Categorías
				Más información sobre Matrices and Arrays en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




