Replace nested loops with a matrix

5 ビュー (過去 30 日間)
Mohammad Ezzad Hamdan
Mohammad Ezzad Hamdan 2021 年 5 月 9 日
I have to optimize this code but i am not sure how to turn it into a matrix.
for i=2:nhx-1
for j=2:nhy-1
U(i,j)= U(i,j)-dt*(Pc(i+1,j)-Pc(i-1,j))/(2*hx);
end
end

採用された回答

Bruno Luong
Bruno Luong 2021 年 5 月 9 日
i=2:nhx-1;
j=2:nhy-1;
U(i,j)= U(i,j)-dt*(Pc(i+1,j)-Pc(i-1,j))/(2*hx);
  1 件のコメント
Mohammad Ezzad Hamdan
Mohammad Ezzad Hamdan 2021 年 5 月 9 日
Thank you!

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

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2021 年 5 月 9 日
Unew = zeros(nhx, nhy);
Unew(2:nhx-1, 2:nhy-1)=U(2:nhx-1, 2:nhy-1)-dt.*(P(3:nhx,2:nhy-1)-P(1:nhx-2,2:nhy-1))./(2*hx);
  1 件のコメント
Mohammad Ezzad Hamdan
Mohammad Ezzad Hamdan 2021 年 5 月 9 日
Thank you!

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by