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 件)
Star Strider
2019 年 2 月 5 日
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.
カテゴリ
ヘルプ センター および 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!