How to speed up the computing time?

5 次查看(过去 30 天)
Dear guys! My code is computationally to expensive and I'm looking for your help.
I have 3 input table with different legths in which each column represents:
1- position in X
2- position in Y
3- timestamp (in nanosec)
What I'm looking for is to create one table with the position in X and Y of the three tags with a common timestamp.
To do so I find the absolute maximum and minimum of the timestamps and then I create a coloumn vector as it follows A=min:seconds(5):max.
At this point, for each interval with the find function I check how many timestamp of the three tags belongs to it (es. find(A(i) > = tag3(:,3) & A(i+1)< tag3(.,3)). The output of the find thanks to the mean function allows me to calculate the mean position in X and Y inside this time interval.
I tested this procedure with a subset of my real data set and everything is as expected but unfortunately using my entire dataset It takes hours to be fully computed.
How could I speed up the entire running time?
Thanks to all of you in advance for your time ! :)
  1 个评论
Julius Muschaweck
Julius Muschaweck 2021-7-19
Did you check out the "Run and Time" feature, under "HOME" -> "CODE", to determine where your code really spends its time?

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2021-7-19
Consider turning your table into a timetable and calling retime on the timetable.
  3 个评论
Riccardo Tronconi
Riccardo Tronconi 2021-7-19
Ok I did it as you suggested but I have two questions:
1- TT2 = retime(TT,'hourly','mean') % I used 'secondly' but how to set second(5)?
2- it is possible that my tags are present in the same time frame/bin or not. Of course, I would display this scenario. Look horizon.mat. Otherwise how can I calculate the Euclidean distance between the mean positions in X and Y?
Steven Lord
Steven Lord 2021-7-19
For question 1, it depends on which release you're using. I forget exactly when the 'regular' and 'TimeStep' input arguments were introduced but if your release has then you could use that syntax.
"TT2 = retime(TT1,'regular',method,'TimeStep',dt) calculates regularly spaced row times using the time step dt."
If not you could use the newTimes input argument.
For question 2, I'm not sure what specifically you mean.
rng default
initialTimes = minutes(randi(60, 7, 1));
rng(1234, 'twister')
x = randi([-10 10], 7, 1);
newtimes = minutes(0:10:60).';
myTimetable = timetable(initialTimes, x)
myTimetable = 7×1 timetable
initialTimes x ____________ __ 49 min -6 55 min 3 8 min -1 55 min 6 38 min 6 6 min -5 17 min -5
result = retime(myTimetable, newtimes, @mean)
result = 7×1 timetable
initialTimes x ____________ ___ 0 min -3 10 min -5 20 min NaN 30 min 6 40 min -6 50 min 4.5 60 min NaN
The first bin in result averages the values for x in rows 3 and 6 of myTimetable since both 6 min and 8 min are in the range [0 min, 10 min).
No row in myTimetable has an initialTimes value in the range [20 min, 30 min) and so the corresponding row of result has a NaN.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-7-19
discretize() all of the A values at the same time, getting out group number G
Now you can
grpstats([X(:), Y(:)], G, @(x) mean(x,1))
and the result should be a max(G) by 2 array where first column is mean of X for the group and second column is mean of Y for the group.
  2 个评论
Riccardo Tronconi
Riccardo Tronconi 2021-7-19
Could you be more specific? I did not understand
Walter Roberson
Walter Roberson 2021-7-19
Assuming that A is a vector in monotonic increasing order:
G = discretize(tag3, A);
XYmean_per_group = grpstats([X(:), Y(:)], G, @(x) mean(x,1));
Xmean_per_group = XYmean(:,1);
Ymean_per_group = XYmean(:,2);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by