Counting occurrence of elements in an array
68 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mustafa Sheikh
el 25 de Mzo. de 2015
Comentada: Katherine May
el 4 de Oct. de 2024
How would I go about counting the occurrence of elements in an array including elements that may not be in the array.
For example if x = [2 3 2 4 5 6 8 2 9 5], I would like to produce an array that has the frequency of each element from 1 to 10 so it'd be output = [0 3 1 1 2 1 0 1 1 0]
Thanks
0 comentarios
Respuesta aceptada
Star Strider
el 25 de Mzo. de 2015
Editada: Star Strider
el 25 de Mzo. de 2015
Use the hist function:
x = [2 3 2 4 5 6 8 2 9 5];
binc = [1:10];
counts = hist(x,binc);
result = [binc; counts]
produces:
result =
1 2 3 4 5 6 7 8 9 10
0 3 1 1 2 1 0 1 1 0
1 comentario
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!