how to aviod the two loops

2 vues (au cours des 30 derniers jours)
Qiu Xu
Qiu Xu le 18 Mai 2024
Réponse apportée : Voss le 18 Mai 2024
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!

Réponses (1)

Voss
Voss le 18 Mai 2024
[X,Y] = meshgrid(x,y);
A = [X(:),Y(:)];

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by