Naveen Kumar Mishra
Naveen Kumar Mishra

Reputation: 45

Saving arrays of every iteration of for loop

I have the following matlab code:

clc;
clear all;
close all;
format long;
d=717;              
r1=60000;             
r2=[61043; 62000];             
Rx=[0; 10];                   
Ry=[11.212; 1];              
k=-1.000953;           
for o=1:1:size(Rx)
i=1;
for rho1=0:17.7:d
    for theta=0:10:360;
        rho=rho1;
        x(i)=rho.*cosd(theta)+(Rx(o)*1000);
        y(i)=rho.*sind(theta)+(Ry(o)*1000);
        xx(i)=(((x(i))-(Rx(o)*1000))/d);
        yy(i)=(((y(i))-(Ry(o)*1000))/d);
        l(i)=sqrt(x(i).^2+(y(i)).^2);
        c1=1/r1;
        s11(i)=c1*(l(i).^2);
        s12(i)=1+(sqrt(1-(1+k)*c1*c1*(l(i).^2)));
        s1(i)=s11(i)/s12(i); %hyperbola sag
        s2(i)=(r2(o)-sqrt((r2(o).^2)-(l(i).^2))); %Best fit sag
        m(i)=abs((-s1(i)+s2(i)));
        i=i+1;
    end
end
dz=m';
xn=xx';
yn=yy';
Z=[dz xn yn];
end

After each iteration of for loop, the arrays dz, xn, yn, and matrix Z get modified. How to save the output of every iteration?

Upvotes: 0

Views: 63

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

You can use the concept of Multidimensional Arrays as follows:

clc;
clear all;
close all;
format long;
d=717;              
r1=60000;             
r2=[61043; 62000];             
Rx=[0; 10];                   
Ry=[11.212; 1];              
k=-1.000953;           
for o=1:1:size(Rx)
    i=1;
    for rho1=0:17.7:d
        for theta=0:10:360;
            rho=rho1;
            x(i)=rho.*cosd(theta)+(Rx(o)*1000);
            y(i)=rho.*sind(theta)+(Ry(o)*1000);
            xx(i)=(((x(i))-(Rx(o)*1000))/d);
            yy(i)=(((y(i))-(Ry(o)*1000))/d);
            l(i)=sqrt(x(i).^2+(y(i)).^2);
            c1=1/r1;
            s11(i)=c1*(l(i).^2);
            s12(i)=1+(sqrt(1-(1+k)*c1*c1*(l(i).^2)));
            s1(i)=s11(i)/s12(i); %hyperbola sag
            s2(i)=(r2(o)-sqrt((r2(o).^2)-(l(i).^2))); %Best fit sag
            m(i)=abs((-s1(i)+s2(i)));
            i=i+1;
        end
    end

    dz(:,:,o)=m';
    xn(:,:,o)=xx';
    yn(:,:,o)=yy';
    Z(:,:,o)=[dz(:,:,o) xn(:,:,o) yn(:,:,o)];
end

Upvotes: 1

Related Questions