Counting certain values from an array

Suppose we have an array r=(2,2,2) filled with zeros and ones. How to count up how many zeros and how many ones do we have?

 Respuesta aceptada

Image Analyst
Image Analyst el 22 de Sept. de 2016
Try this:
numOnes = sum(r(:))
numZeros = numel(r) - numOnes

4 comentarios

Vadim Tambovtsev
Vadim Tambovtsev el 22 de Sept. de 2016
No, my array in more complicated. Array (m,n,k), contans "a" and "b" values. How many "a" and "b" are there?
Image Analyst
Image Analyst el 22 de Sept. de 2016
Editada: Image Analyst el 22 de Sept. de 2016
You could have sped this along by two hours by saying that first instead of saying the array is "filled with zeros and ones". So to count the number of a's and b's:
numA = length(find(r==a));
numB = length(find(r==b));
There are other ways, but this is fairly simple and compact.
Vadim Tambovtsev
Vadim Tambovtsev el 22 de Sept. de 2016
Editada: Image Analyst el 22 de Sept. de 2016
quality control:
dd=[ 3 7; 4 5]
num7=sum(find(dd==7))
output:
num7= 3
Why is it 3? isn't it supposed to be 1, because there is 1 "7" in the array?
Image Analyst
Image Analyst el 22 de Sept. de 2016
Use length() instead of sum().

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numerical Integration and Differential Equations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 22 de Sept. de 2016

Editada:

el 22 de Sept. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by