How to speed up simple sorting
Mostrar comentarios más antiguos
Hello, I got two variables (Units and time points) that needs to me sorted with respect to each other. Meaning, I want to sort all the time points to its specific Unit. With large numbers of Units and many time points this sorting takes forever. Is there a way to speed this up?
What I got so far works, but it is rather slow:
%Prepare example RAW data
Unit=int32(randi(60,5000000,1)); %60 Units in an order, that matches the timepoints
timepoints=randi(600,5000000,1); %5*10^6 Timepoints (from a range of 600ms) in an order, that corresponds to the according unit
%Prepare the variable where the data should be sorted
for i=1:60
Unit_sort{i,1}=i;
Unit_sort{i,2}=[];
end
%Sort "time points" to the according "Unit"
for i=1:length(timepoints)
index = find([Unit_sort{:,1}] == Unit(i));
Unit_sort{index,2}=[Unit_sort{index,2} timepoints(i)];
end
2 comentarios
Jan
el 8 de Feb. de 2017
Typo: "Units(i)" must be "Unit(i)". But after fixing this, the code fails, because find([Unit_sort{:,1}] == Units(i)) must be empty from the beginning.
Please post some code, which performs, what you want to achieve.
Respuesta aceptada
Más respuestas (1)
Steven Lord
el 8 de Feb. de 2017
I think what you're trying to do is sortrows.
% Sample data
A = randi(10, [20 2]);
% Sort by column 1, use column 2 to break ties
B = sortrows(A);
% Sort by column 2, use column 1 to break ties
C = sortrows(A, [2 1]);
% Show all three side-by-side
T = table(A, B, C)
1 comentario
Felix Ludwig
el 9 de Feb. de 2017
Categorías
Más información sobre Shifting and Sorting Matrices 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!