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 ; endIf 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 SolutionsMATLAB: Plot3 out of loop clear;theta=0:10:360;rho=0:.5:2;L=-2:0.5:2;[a,b,c]=meshgrid(theta,rho,L);[x,y,z]=pol2cart(a,b,c);plot3(x(:),y(:),z(:)); MATLAB: Shaded surface St11=100;Sc11=St11*2/3;St22=80;Sc22=St22*2/3;S=60;i1 = 0;r = NaN(101,51);for phi=0:pi/50:2*pi i1 = i1+1; i2 = 0; if cos(phi)>=0 X=St11; else X=Sc11; end if sin(phi)>=0 Y=St22; else Y=Sc22; end for psi=-pi/2:pi/50:pi/2 i2=i2+1; F=@(r0)(((cos(phi)*cos(psi)*r0)/X)^2-(cos(phi)*cos(psi)*r0*sin(phi)*cos(psi)*r0)/(X^2)+((sin(phi)*cos(psi)*r0)/Y)^2+(r0*sin(psi)/S)^2-1)^2; r(i1,i2)=abs(fminsearch(F,0)); endend[psi,phi] = meshgrid(-pi/2:pi/50:pi/2,0:pi/50:2*pi);x=cos(psi).*cos(phi).*r;y=cos(psi).*sin(phi).*r;z=sin(psi).*r;surf(x,y,z) Related QuestionError using plot3 – Vectors must be the same lengthPlotting multi-radius spherical mesh
Best Answer