slow for and while loops

4 ビュー (過去 30 日間)
sani
sani 2020 年 1 月 21 日
編集済み: Guillaume 2020 年 1 月 24 日
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;
tic
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;
break;
end
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;
end
if T2.time(i) < T1.time(j)
break;
end
end
end
toc
  12 件のコメント
sani
sani 2020 年 1 月 24 日
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
Guillaume 2020 年 1 月 24 日
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.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2020 年 1 月 24 日
編集済み: Guillaume 2020 年 1 月 24 日
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
  2 件のコメント
sani
sani 2020 年 1 月 24 日
great, it seems to be working!
there is a way I'll be able to see the time format as HH:mm:sssssssss?
Guillaume
Guillaume 2020 年 1 月 24 日
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'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by