Making a for loop that appends outputs in a matrix/array

Hello,
I am trying to figure out how to compose a "for" loop that will append outputs at the end of each loop to a matrix. It basically outputs a series of 4 columns by 66049 rows which I want it to do for "k" times. I have kept the parts I am struggling with with capital letter comments.
The three parts I am struggling with are as follows:
1) I need the code to read every 103041 rows of the "sorted" matrix in order to create a different "layer" matrix. So for i=1 it would read from 1:103041, i=2 from 103042:206082 etc.
2) For the expression Zi=Zun(1,1); I need it for i:1 to read the first row of "Zun", then for i:2 to read the second row etc.
3) Finally, at the end of the code the output is a series of 66049 rows by 4 columns of data. I need the code to append each output in the same matrix. As in the next output of the loop for i:2 will go from rows 66050 to 132098 and so on.
I thought about making 33 different matrices (one from each output) and then combining them at the end but this seems counterintuitive.
%Start loop
k=size(Zun,1); %Get number of layers
For i=1:k
%Get one layer of the unique matrix
%Sort everything in a specific array to make the meshgrid afterwards
layer=sorted(1:103041,[1 2 4]); %NEEDS ADJUSTMENT FOR LOOP
x=unique(layer(1:end,1));
tlength=size(x,1);
x=x';
y=unique(layer(1:end,2));
y=y';
t=layer(1:tlength,3);
t=t';
Zi=Zun(i,1); %NEEDS ADJUSTMENT FOR LOOP
%Make a meshgrid from the layer
[X,Y] = meshgrid(x,y);
T=meshgrid(t);
%------------------------------------------
%Create query grid
%Create meshgrid
Xinit = 0:3125:800000;
Yinit = 0:3125:800000;
[Xq,Yq] = meshgrid(Xinit,Yinit);
%Create vector matrix
XY = [Xq(:) Yq(:)];
theta=10; %rotate clockwise by theta
R=[cosd(theta) -sind(theta); sind(theta) cosd(theta)]; %rotation matrix
rotXY=XY*R'; %multiply vector by rotation matrix
Xqr = reshape(rotXY(:,1), size(Xq,1), []);
Yqr = reshape(rotXY(:,2), size(Yq,1), []);
%apply shift
Xqrs = Xqr+175536.2;
Yqrs = Yqr+36617.62772;
%---------------------------------------------------
finalT=interp2(X,Y,T,Xqrs,Yqrs,'spline');
%plot data (I skip this part to keep the message short)
%----------------------------------------------------
%put Xfin,Yfin,Zfin,Tfin into columns
Xfin=reshape(Xq',[],1);
Yfin=reshape(Yq',[],1);
Tfin=reshape(finalT',[],1);
Zfin=Zi*ones(size(Tfin));
%put X,Y,Z,Tfin in one matrix
Layerout=[Xfin Yfin Zfin Tfin]; %THIS NEEDS TO BE APPENDED EACH TIME
end
Thank you for your help,
Pavlos

 採用された回答

James Tursa
James Tursa 2018 年 12 月 14 日

0 投票

"... I thought about making 33 different matrices (one from each output) and then combining them at the end but this seems counterintuitive..."
That's actually not a bad way to do it at all. Just stuff them into a cell array and then concatenate them all at once at the end.

1 件のコメント

Pavlos Farangitakis
Pavlos Farangitakis 2018 年 12 月 17 日
I think that is what I will go with in the end. Any idea how to tackle questions 1 and 2?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by