How to remove for loops from this code

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.

回答(1 个)

I’m not certain what you want to do, although my best guess is that you want to convert your polar coordinates to Cartesian.
There’s a function for that called pol2cart (link).
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.

类别

在 帮助中心 和 File 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