MATLAB: Pol2cart in loop and pre-allocating

plo2cart pre allocating

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

Best Answer

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
Related Question