Keep a tally of the number of occurences of a value in an array
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
FredMat
el 17 de Oct. de 2022
Comentada: FredMat
el 17 de Oct. de 2022
Hello,
I have an array with integers from 1 to n. I would like to create a second array with the occurence number of the value in the corresponding location (index) of the initial array. For instance, if I have a first array [2 3 1 2 1 4 3 2 4], I want to generate a second array [1 1 1 2 2 1 2 3 2].
I'd like to avoid a series of n conditional statements.
Thx!
1 comentario
Jiri Hajek
el 17 de Oct. de 2022
Hi, you don't need conditional statements, but a loop is probably necessary... You can use logical indexing:
LogicalIndexes = FirstArray == i;
SecondArray(LogicalIndexes) = nnz(LogicalIndexes);
Respuesta aceptada
Más respuestas (0)
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!