updating/inserting summary array
Mostrar comentarios más antiguos
hi, i have a summary array where it keeps a count of repeated items. e.g
sumarray = {'red', 2; ...
'blue', 3}
etc. While running a for loop, if a red item appears, I simply want to increment the count to 3 for 'red', if a new category comes in e.g. 'pink' I simply want to add a new row {'pink',1}. What is the most efficient way to implement this?
Thanks.
1 comentario
Respuesta aceptada
Más respuestas (1)
You would have to test, but here is what I guess is an efficient solution:
colors = {'red', 'black', 'red', 'green', 'red', 'green', 'red'} ;
[uc,~,ic] = unique( colors ) ;
counts = accumarray( ic, 1 ) ;
sumarray = [uc.', num2cell(counts)] ;
For the given dummy set of colors, this outputs
>> sumarray
sumarray =
'black' [1]
'green' [2]
'red' [4]
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!