How do you get Matlab to return a value for how many 1's are in a vector containing just 1's and zeros

3 visualizaciones (últimos 30 días)
Like if i have greencars=[11111111000000000000] how to i get Matlab to count how many ones there are?

Respuesta aceptada

Babak
Babak el 29 de Nov. de 2012
>> greencars=[1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
>> numer_of_ones = sum(greencars)

Más respuestas (3)

Adrian Jimenez-Galindo
Adrian Jimenez-Galindo el 29 de Nov. de 2012
You can use Count(greencars, '==1') Hope this helps.

Azzi Abdelmalek
Azzi Abdelmalek el 29 de Nov. de 2012
a=[1 0 1 1 0 0 0]
numel(find(a))

Walter Roberson
Walter Roberson el 29 de Nov. de 2012
nnz(greencars)

Categorías

Más información sobre Get Started with MATLAB 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