hi all,
how can I put [x,y,z]=pol2cart(theta,rho,z), in tripple loop for different values of heta,rho,z, as well how to pre allocate [x,y,z]?
Regards

 채택된 답변

KSSV
KSSV 2020년 6월 9일

0 개 추천

You need not preallocate (x,y,z) when you use the function.....If you want to run it thrice, you can do the following.
X = cell(3,1) ; Y = cell(3,1);Z = cell(3,1) ;
for i = 1:3
[x,y,z]=pol2cart(theta{i},rho{i},z{i}) ;
X{i} = x ; Y{i} = y ; Z{i} = z ;
end
If all the size of theta, rho, z are same. You can use matrices instead of cells.
N = length(theta) ;
X = zeros(N,3) ; Y = zeros(N,3); Z = zeros(N,3) ;
for i = 1:3
[x,y,z]=pol2cart(theta(:,i),rho(:,i),z(:,i)) ;
X(:,i) = x ; Y(:,i) = y ; Z(:,i) = z ;
end

댓글 수: 6

clear
close all
format long
theta=0:45:360;
rho=0:.5:2;
L=-2:2;
X = cell(3,1) ; Y = cell(3,1);Z = cell(3,1) ;
for i=1:length(L)
for j=1:length(rho);
for k=1:length(theta);
[x,y,z]=pol2cart(theta(k),rho(j),L(i));
X{i} = x ; Y{i} = y ; Z{i} = z ;
end
end
end
figure(1)
plot3(x,y,z);hold on
why can not plot the results?
KSSV
KSSV 2020년 6월 9일
편집: KSSV 2020년 6월 9일
clear
close all
format long
theta=0:45:360;
rho=0:.5:2;
L=-2:2;
X = cell(3,1) ; Y = cell(3,1);Z = cell(3,1) ;
figure
hold on
for i=1:length(L)
for j=1:length(rho);
for k=1:length(theta);
[x,y,z]=pol2cart(theta(k),rho(j),L(i));
X{i} = x ; Y{i} = y ; Z{i} = z ;
plot3(x,y,z,'.')
end
end
end
Oday Shahadh
Oday Shahadh 2020년 6월 9일
empty plot also!
Oday Shahadh
Oday Shahadh 2020년 6월 9일
it must be something like the attached plots
KSSV
KSSV 2020년 6월 9일
Check does x, y, z have any values...
Oday Shahadh
Oday Shahadh 2020년 6월 9일
yes there is a values ,it works just in case that insert plot command inside the loop with ('.')
I need to put the plot outside,pls
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2020년 6월 9일

0 개 추천

No need for loop or for preallocating when you just execute for arrays of theta,rho, and Z.
theta=linspace(0,pi,100);
rho=linspace(0,10,100);
Z = linspace(10,100,100);
[x,y,z]=pol2cart(theta,rho,Z);

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 6월 9일

댓글:

2020년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by