how to aviod the two loops

1 回表示 (過去 30 日間)
Qiu Xu
Qiu Xu 2024 年 5 月 18 日
回答済み: Voss 2024 年 5 月 18 日
I have a code:
x=linspace(-10,10,10001);
y=linspace(-5,5,20001);
A=[];
for j=1:length(x)
for k=1:length(y)
A=[A;x(j),y(k)];
end
end
The computation is very slow due to the two loops, so How can I aviod the two loops in this code?
Thanks very much!

回答 (1 件)

Voss
Voss 2024 年 5 月 18 日
[X,Y] = meshgrid(x,y);
A = [X(:),Y(:)];

カテゴリ

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