Comparing Duration Arrays is time consuming - How to improve my script?
10 次查看(过去 30 天)
显示 更早的评论
EDIT 12.9.2019: Due to the first comments i tried to explain my problem more in detail. I will try your suggestions and response in a comment EDIT END
Hello alltogether
i got a problem on my script while comparing duration arrays which is taking a huge amount of time .
What I want to do with my Script: dataG is read form .xls-file and contains time information on start and end times ( format 2 x 1 : 03-Aug-2019 10:06:12). data is read from .csv-file and contains two time vectors (format 1 x n : 9:31:52.000 ...). The time vector from data fits into the time vector from dataG. I want to cut the array from data corresponding to the given start
Here some excerpts from my script:
dataG=readtable(X{posG}); % data with start and end time
...
data=readtable(N{1}); % continous time vector
...
tdur=table2array(dataG(posPG,7:8)); % is read as datetime format
[y, m, d]= ymd(tdur);
tdur=tdur-datetime(y,m,d,0,0,0); % conversion to duration format
clock=table2array(data(:,2)); % is read as duration format
try
for post1=1:length(clock) % Comparing durations - Here is where about 90% of the time is consumed
if tdur(1)<clock(post1)
break
end
end
for post2=post1:length(clock)
if tdur(2)<clock(post2) || isnan(clock(post2 + 1))
break
end
end
catch
end
data(post2:end,:)=[]; %cut data
data(1:post1,:)=[];
Matlab recognizes my variables automatically with 'readtable'.' tp' contains start time and end time.
In order to compare both variables I chose the conversion to duration arrays for both variables. For certain reasons, it is slowing down my whole script which are about 300 more lines and many for-loops and if-conditions. The function is called about 800 times, so time does matter..
The Profiler says, table2array is the most called function, 80-90 % of time on the first for-loop. I am pleased for any sugestion, to make my script faster! Thank you in advance =)
I think
Kind Regards
Rob
3 个评论
Steven Lord
2019-9-4
If you describe at a higher level what you're trying to do with this code, we may be able to offer some guidance to help improve your code to accomplish your goal. Right now you've shown us a low-level view that makes me suspect you want to discretize your duration data, but I can't be certain.
采纳的回答
Fabio Freschi
2019-9-4
Not sure if I understand correctly the problem without data. I try:
idx = find(clock > tdur(1),1,'first')
2 个评论
Fabio Freschi
2019-9-12
Not with find. We can try to make the exercise, but I am not sure if is worth the hassle
更多回答(1 个)
Steven Lord
2019-9-12
So you want to extract only those subset of rows of your data that fall into a certain time span? If so, and if you choose to store your data in a timetable array, use a timerange as your row index.
Depending on what you want to do with the data from that smaller time span, some of the other timetable related functions may be of use (I'm thinking specifically of retime or some of the data preprocessing functions like the grouping functions.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!