How to remove for loops from this code
Ältere Kommentare anzeigen
r=0.5:0.5:4 ;
theta=0: (pi)/4 : 7*(pi)/4 ;
[X,Y] = meshgrid(r, theta);
polar=zeros(64, 2);
for j=1:8
for i=1:8
polar((8*(i-1)) + j, 1)= X(j,i);
polar((8*(i-1)) + j, 2)= Y(j,i);
end
end
cart=zeros(64, 2);
for i=1:64
cart(i, 1)= polar(i,1) * cos(polar(i,2));
cart(i, 2)= polar(i,1) * sin(polar(i,2));
end
cart
Im getting the correct awnser but I need to remove the for loops from it any advice is welcome.
Antworten (1)
Star Strider
am 5 Feb. 2019
I’m not certain what you want to do, although my best guess is that you want to convert your polar coordinates to Cartesian.
Try this:
r=0.5:0.5:4 ;
theta=0: (pi)/4 : 7*(pi)/4 ;
[R,Th] = meshgrid(r, theta);
[X,Y] = pol2cart(Th,R);
figure
plot(X,Y)
axis equal
Experiment to get the result you want.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!