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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by