How do I avoid using if and else statements in this code
古いコメントを表示
I want to rewrite this code but without if and else statements. I just want to enhance convergence rate
for i=1:N-1
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2/dx^2-2/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-(cos(x(i)+y(j))+cos(x(i)-y(j)));
if i==1
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-cos(y(j))/dx^2;
else
A(i+(M-1-j)*(N-1),i-1+(M-1-j)*(N-1))=1/dx^2;
end
end
end
3 件のコメント
Rik
2020 年 5 月 1 日
Why do you want to remove the if? What is your end goal? To speed up the code?
Also, please use the layout tools to make your code more readable.
SA
2020 年 5 月 1 日
Ameer Hamza
2020 年 5 月 1 日
Can you write down the equation you are trying to implement with these for loops?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Structural Mechanics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
