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 Kommentare

Jan
Jan am 8 Feb. 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 8 Feb. 2017
Bearbeitet: Jan am 8 Feb. 2017

0 Stimmen

2 general rules:
  • Pre-allocate the output. The iterative growing of arrays is a Don't!
  • Prefer to run the loop over the smaller array.
uUnit = unique(Unit);
Unit_sort = cell(1, length(uUnit));
for k = 1:length(uUnit)
Unit_sort{k} = timepoints(Unit == uUnit(k));
end
Perhaps this does what you want - I'm not sure because your posted code does not run and I cannot guess, what the wanted result is. But if it runs, read the docs of splitapply and accumarray.

2 Kommentare

Felix Ludwig
Felix Ludwig am 9 Feb. 2017
Thank you for your help, I corrected the code I provided - now it works (In the old slow way).
I will try to apply your suggestions now and see, if it speeds up the procedure ...
Felix Ludwig
Felix Ludwig am 9 Feb. 2017
ok, I tried your code and indeed its waaaay faster. Thank you! Note to myself: Always run the loop over the smaller array!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 8 Feb. 2017

0 Stimmen

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 Kommentar

Felix Ludwig
Felix Ludwig am 9 Feb. 2017
Thank you for your input, It's not exactly what I meant. I want to have for example 60 unique Units and sort a high number of timepoints to the unit they belong to. I updated the provided code above. Now it works - in the original slow way. But I will try to apply the sortrow procedure to my problem.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by