Sum and Sort Values from Multiple Vectors
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Patrick Rungrugeecharoen
el 26 de Mayo de 2019
Comentada: Patrick Rungrugeecharoen
el 26 de Mayo de 2019
I have a numerical vector:
Time = [10 23 54 50]
This corresponds to the string vector:
Place = [A B C A]
And another string vector:
Unique = [A B C]
How do I count these so that I get the following result:
x = [60 23 54]
This is just a snippet of the actual data - the full data set is at least 5000 long. I figured an if loop would work but I'm having trouble visualising and writing the code out.
Any help would be much appreciated. Thanks :)
2 comentarios
per isakson
el 26 de Mayo de 2019
Editada: per isakson
el 26 de Mayo de 2019
A B C are they names of variables or what?
Respuesta aceptada
KSSV
el 26 de Mayo de 2019
Time = [10 23 54 50]
place = {'A' 'B' 'C' 'A'}
place_unique = unique(place)
N = length(place_unique) ;
iwant = zeros(N,1) ;
for i = 1:N
iwant(i) = sum(Time(strcmp(place,place_unique{i}))) ;
end
iwant
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!