How to reference previous value in an array without a for loop?

9 ビュー (過去 30 日間)
HWIK
HWIK 2021 年 8 月 24 日
コメント済み: Wan Ji 2021 年 8 月 24 日
Hi,
I have a for loop set up following a similar structure to this:
for i=2:size(data,1)
data.Var3(i)=interp1(ref_data.x,ref_data.y,data.Var2(i))-interp1(ref_data.x,ref_data.y,data.Var2(i-1));
end
Where I use a reference table to interpolate a certain x value from my current data to obtain a corresponding y value, that is done twice per row.
It works, but the data table I'm using has 300,000 rows, so the run time of this loop alone is just under 3 mins in my case (data.Var3 is not being resized after every iteration in the loop).
Doing this operation this way simply takes too long, is there any way you can think of in which I can carry out the operation inside the for loop, without actually putting it inside a loop?
Any help is appreciated,
Thanks!

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 24 日
use i=2:size(data,1); data.Var3(i)=interp1(ref_data.x,ref_data.y,data.Var2(i))-interp1(ref_data.x,ref_data.y,data.Var2(i-1));
  2 件のコメント
HWIK
HWIK 2021 年 8 月 24 日
It was that easy... Thanks!
Wan Ji
Wan Ji 2021 年 8 月 24 日
my pleasure, :)

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by