How can I decrease the running time of this code?

1 回表示 (過去 30 日間)
evelyn parra
evelyn parra 2019 年 4 月 14 日
回答済み: Star Strider 2019 年 4 月 14 日
for i=1:1:f-1
Ux(i,:)=X(i+1,:)-X(i,:)
Uy(i,:)=Y(i+1,:)-Y(i,:)
Uz(i,:)=Z(i+1,:)-Z(i,:)
end

回答 (1 件)

Star Strider
Star Strider 2019 年 4 月 14 日
Use the diff (link) function:
Ux = diff(X);
Uy = diff(Y);
Uz = diff(Z);
When I timed your code and diff with these matrices:
f = 1000;
X = rand(f, 500);
Y = rand(f, 500);
Z = rand(f, 500);
the results were:
Elapsed time is 0.633127 seconds. % Your Code
Elapsed time is 0.007197 seconds. % ‘diff’
The resulting matrices were the same.
Experiment to get the result you want.

カテゴリ

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