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