MATLAB Answers

slow for and while loops

7 views (last 30 days)
sani on 21 Jan 2020
Edited: Guillaume on 24 Jan 2020
Hi all, I'm running those lines that suppose to run on two tables (T2 and T1) and check if the difference in the first column is less than coinc_win. both columns in the tables are data from the same clock and I want the code to match line from table T2 to the correspond lines in table T1 and write the lines to T3. this is what I've written so far, but it runs pretty slow, anyone has suggestions how to improve it?
coinc_win = 500;
correction = 150;
j = 1;
r = 1;
for i = 1: height(T2(:,1))
while j <= height(T1(:,1))
while (T2.time(i)+correction) - T1.time(j) > coinc_win || T2.time(i)+correction > T1.time(j)
j = j+1;
while T2.time(i) - T1.time(j) < coinc_win && (T2.time(i)+correction) > T1.time(j)
T3.deltaT(r) = T2.time(i) - T1.time(j);
T3.energy_G(r) = T2.E(i);
T3.energy_S(r) = T1.E(j);
r= r+1;
j = j+1;
if T2.time(i) < T1.time(j)


Show 9 older comments
Mohammad Sami
Mohammad Sami on 24 Jan 2020
timetables can accept either datetime format or duration format. you can convert to duration as follows.
H = 0;
MI = 0;
S = 0;
MS = NS ./ 1000; % NS can be an array of nanoseconds
D = duration(H,MI,S,MS)
sani on 24 Jan 2020
when I try to applay thet the times not fit.
the first line in T2 should have timestemp of 0.084241514 sec and instead it is 00:01:24
Guillaume on 24 Jan 2020
A much simpler way to convert a numeric array in nanoseconds to a duration type is with:
dur = seconds(NS * 1e-9);
See example code in my answer.

Sign in to comment.

Accepted Answer

Guillaume on 24 Jan 2020
Edited: Guillaume on 24 Jan 2020
After looking at it a bit more closely, what you're trying to do is a bit too advanced for synchronize. I would still recommend that you convert to timetable at the end, but for your synchronisation, you'll have to do it manually. Easiest is with ismembertol:
matchwindow = 500; %matching window within which two events are considered equal, in nanoseconds.
S_offset = 150; %offset for S table, in nanoseconds.
G = readtable('T1.txt'); %I would recommend a better name than T1 or G. A name that clearly describes what the table represent
G.Properties.VariableNames = {'Time_G', 'Energy_G'};
S = readtable('T2.txt'); %I would recommend a better name than T1 or S. A name that clearly describes what the table represent
S.Properties.VariableNames = {'Time_S', 'Energy_S'}; %using different variable names so that the tables can be horizontally concatenated
%find intersection of time within the matchwindow and merged the matching rows into a new table
[ismatch, matches] = ismembertol(S.Time_S + S_offset, G.Time_G, matchwindow, 'DataScale', 1); %DataScale is one to make the tolerance absolute
merged = [G(matches(ismatch), :), S(ismatch, :)];
merged.Delta = merged.Time_S - merged.Time_G;
%convert to timetable
merged = convertvars(merged, {'Time_G', 'Time_S', 'Delta'}, @(var) seconds(var * 1e-9));
merged = table2timetable(merged, 'RowTimes', 'Time_G')
edit: spelling


sani on 24 Jan 2020
great, it seems to be working!
there is a way I'll be able to see the time format as HH:mm:sssssssss?
Guillaume on 24 Jan 2020
HH:mm:sssssssss is not a valid duration format. But you can certainly set the format of the durations to anything valid, e.g.:
merged.Time_G.Format = 'hh:mm:ss.SSSSSSSSS'

Sign in to comment.

More Answers (0)

Sign in to answer this question.