Number of counts in matrix

2 visualizaciones (últimos 30 días)
Prince
Prince el 3 de Abr. de 2024
Comentada: Voss el 3 de Abr. de 2024
How to count the number of individual elements in a matrix using matlab? I mean the number of times individual elements appear.

Respuesta aceptada

Voss
Voss el 3 de Abr. de 2024
You can use nnz.
Example:
A = [1 1 2; 2 1 2; 3 1 1]
A = 3x3
1 1 2 2 1 2 3 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
nnz(A == 1) % number of times 1 appears in A
ans = 5
nnz(A == 2) % number of times 2 appears in A
ans = 3
nnz(A == 3) % number of times 3 appears in A
ans = 1
  4 comentarios
Prince
Prince el 3 de Abr. de 2024
It worked. Thank you so much
Voss
Voss el 3 de Abr. de 2024
Great! You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 3 de Abr. de 2024
Use histcounts with unique (or uniquetol) to generate the bins. You will need to add one more bin at the end since the last bin includes both its edges (unlike the other bins, which only include their left edges.) I would use inf as the right-most end of that last bin.

Categorías

Más información sobre Debugging and Analysis 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