Difficulty in Concatenating the matrix and preallocation- I would like to concatenate marix output generated from each iteration one after the other but my matlab code add up matrix each time from the beginning. How can i change it.Please help-Thanks

Data1=('uComponent.txt');
Data2=('vComponent.txt');
Data3=('wComponent.txt');
u=importdata(Data1);
v=importdata(Data2);
w=importdata(Data3);
[r1,c1]=size(u)
TM=[];T=[];Mu=[];Mv=[];Mw=[];
k=0;
x=10.0658; y=14;
x01=-4.2757; y01=-1.376;
[M,N]=myfun1(x,y,x01,y01);
R=reshape(M,5,6);
S=reshape(N,5,6);
W= [-pi -pi*3/4 -pi/2 -pi*1/4 pi/4 pi/2 pi*3/4 0];
for i=1:27:r1;
M1=u(i+13:i+17,7:12);
M2=v(i+13:i+17,7:12);
M3=w(i+13:i+17,7:12);
for D= 0:9:27
[xn,yn]=pol2cart(W,D);
Z1=interp2(R,S,M1,xn,yn);
Mu=vertcat(Mu,Z1);
end
for D=0:9:27;
[xn,yn]=pol2cart(W,D);
Z2=interp2(R,S,M2,xn,yn);
Mv=vertcat(Mv,Z2);
end
for D=0:9:27;
[xn,yn]=pol2cart(W,D);
Z3=interp2(R,S,M3,xn,yn);
Mw=vertcat(Mw,Z3);
end
T=[T;Mu;Mv;Mw];
k=k+1;
end
My questions are: How can i preallocate memory in case of T, Mu,Mv and Mw? It seems size of Mu, mv and Mw keeps on increasing for each iteration of i, althouhg the size of M1, M2 and M3 stays the same. How can i change the code so that i can the size of Mu,Mv and Mw stays the same for each iteration for i, and just changes the value as the value of M1,M2 and M3 changes. Thank you in advances. All tips and help will be appreciated.

 Accepted Answer

You do not need the "for D" loop:
W = [-pi -pi*3/4 -pi/2 -pi*1/4 pi/4 pi/2 pi*3/4 0];
D = 0 : 9: 27;
[Dgrid, Wgrid] = ndgrid(D, W);
[xn, yn] = pol2cart(Wgrid, Dgrid);
Z1 = interp2(R,S,M1,xn,yn);
Z2 = interp2(R,S,M2,xn,yn);
Z3 = interp2(R,S,M3,xn,yn);
I suspect that you made a mistake in your code. Your Mu, Mv, Mw are getting larger for each i iteration, and you put the ever-increasing versions of those on the end of T. I suspect that you want
Mu=[];Mv=[];Mw=[];
within the for i
If I am correct then this will make a difference to how to preallocate the memory.

1 Comment

The syntex ndgrid and leaving out the 'For D loop'makes my code considerably shorter. Thank you for your help Roberson. The execution time has reduced to almost half of what it took before. Now i could go even without a preallocation of required memory. Thank you..

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!