help with inefficient code

1 回表示 (過去 30 日間)
sani
sani 2020 年 1 月 19 日
コメント済み: Les Beckham 2020 年 1 月 21 日
Hi, I have those lines in my script that they purpuse is to fix jumps in a clock (it runs on a table). for instance this is the data:
1913834560
1993310708
177158681 + 1993310708
270995495 + 1993310708
2036984230 + 1993310708
2057526148 + 1993310708
2064917157 + 1993310708
2089319288 + 1993310708
6799572 +2089319288 + 1993310708
13663870 +2089319288 + 1993310708
in this case the jump is in the bolt line and the correction will be to add the bolt value to all the later values until the next jump (as sown in the bold summing), and so on.
I write this code, but it takes forever to run on ~500K lines, any ideas to improve it?
thanks!
prev = 0; %time linearity
sum = 0;
for i = 2:height(T1(:,1))
if prev > T1.time(i)
sum = sum + prev;
end
prev = T1.time(i);
T1.time(i) = T1.time(i)+sum;
end

採用された回答

Les Beckham
Les Beckham 2020 年 1 月 19 日
編集済み: Les Beckham 2020 年 1 月 19 日
Try this.
There may be a more efficient approach but I'm pretty sure this will be faster than what you have now.
T1.time = [
1913834560
1993310708
177158681
270995495
2036984230
2057526148
2064917157
2089319288
6799572
13663870 ];
del = diff(T1.time);
idx = find(del<0);
sum = zeros(size(T1.time));
for i=1:length(idx)
sum(idx(i)+1:end) = sum(idx(i)+1:end) + T1.time(idx(i)-1);
end
T1.time_adjusted = T1.time + sum;
% plot to visualize results
plot(1:length(T1.time), T1.time, 1:length(T1.time), T1.time_adjusted)
grid on
  2 件のコメント
sani
sani 2020 年 1 月 19 日
thanks! it run much faster than before!
Les Beckham
Les Beckham 2020 年 1 月 21 日
You are welcome. Glad that it helped.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by