pol2cart in loop and pre-allocating

2 vues (au cours des 30 derniers jours)
Oday Shahadh
Oday Shahadh le 9 Juin 2020
Commenté : Oday Shahadh le 9 Juin 2020
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

Réponse acceptée

KSSV
KSSV le 9 Juin 2020
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 commentaires
KSSV
KSSV le 9 Juin 2020
Check does x, y, z have any values...
Oday Shahadh
Oday Shahadh le 9 Juin 2020
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

Connectez-vous pour commenter.

Plus de réponses (1)

David Hill
David Hill le 9 Juin 2020
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);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by