Saving matrix after each for loop, note size of n(k) is not constant

Dear all, I have to save the vector after each loop, here size of n(k) varies. I am not able to save the values of Z after each loop
alpha=(17*pi)/180; %slope angle
dz=2; %depth of water table from the suface
dlz=4; % toltal depth of slope
fi=(25*pi)/180; %friction angle
C=4; %4 kpa=0.4 N/cm^2
gsat=21; %21 kN/m^3=0.021 N/cm3
gw=9.8; %9.81 Kn/m^3=0.0098N/cm3
Dzero=0.0004; %0.0004m^2/s=4 cm2/s
ksat=1e-5; %e-5 m/s
iz=7.2/1000; %iz=7.2 mm/h= 2e-4 cm/s
deltat=10:10:600;
q1=Dzero*cos(alpha)*cos(alpha);
q2=deltat*q1;
deltaz=sqrt(q2);
r=q2./deltaz.^2;
n=round(dlz./(deltaz)+1);
% deltat=60; %time step
% deltaz=0.1482; %space step
% T=12*60*60; %time duration of rainfall= 12h to seconds
% r=(deltat/deltaz^2)*Dzero*cos(alpha)*cos(alpha);
% n=27; %time step, based on T
for k=1:60;
Z=linspace(deltaz(k),4,n(k));% depth in cm
mat(k,:)=Z
% Z=Z';
% display (Z)
end

Answers (2)

Use a cell array instead,
mat{k}=Z;

2 Comments

Dear Matt,
The command is not working.
Thank you for the reply
I can't quite work out from glancing at your code exactly what n is, but can you not just calculate the n(k) ahead of the loop and presize a result array (using zeros or NaNs if zero is a valid value for your results) based on the largest of the n(k) values and fill only those elements of the array required for each k?

1 Comment

Dear Adam,
Actually the value of n varies, where n is number of nodes in finite difference method. I want to calculate the value of Z vector for every n(k). Thank you.

This question is closed.

Asked:

on 20 Oct 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!