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.

Answers (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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 5 Feb 2019

Answered:

on 5 Feb 2019

Community Treasure Hunt

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

Start Hunting!