for-loop returns vector of 1xi when all input data are vectors of ix1....why does it do this? Does it matter?
2 views (last 30 days)
Show older comments
It doesn't seem to matter with the calculation of my for-loop, as the results are correct, but I am wondering why my for-loop automatically returns vectors with the dimensions of 1xi rather than ix1. Is this mathlab's default mode? I know you can force the vector dimensions to change with the follow type of equation,
as=As';
but I would like to understand how mathlab knows to move "right" along a 1xi vector and also move "down" along a ix1 vector within the same for-loop. This is what seems to be happening, and again the results are successful, but is this a "problem" that just happens to not be causing trouble? Thank you.
%ESCIMO energy balance method for snow melting.
%Data from the ... Switzerland
clear all
x=load('C:\Users\wesser\Desktop\P2\ESCIMO\ESCIMOdata.dat'); %Load the file with data
%Field Observed Parameters
td=x(:,4); %hour of the day
T=x(:,5); %Air Temperature (K)
Relhum=x(:,6); %Relative Humidity (%)
Wind=x(:,7); %Wind Speed (m/s)
P=x(:,8); %Precipitation (mm/h)
GlobalRad=x(:,9); %Global Radiation (W/m2)
Inlong=x(:,10); %Incoming Longwave Radiation (W/m2)
Snow_obs=x(:,11); %SWE Observed
%Parameters (to be CALIBRATED)
PM=importdata('C:\Users\wesser\Desktop\P2\ESCIMO\parameter.txt'); % model parameters input
Amin=PM(1,:); %Minimum Albedo
Aadd=PM(2,:); %Additive Albedo
DPp=PM(3,:); %Decline parameter (Positive temperatures)
DPn=PM(4,:); %Decline parameter (negative temperatures)
SS=PM(5,:); %Significant snowfall (mm/h)
Tp=PM(6,:); %Phase transition Temperature (K)
Se=PM(7,:); %Snow Emissivity
SHF=PM(8,:); %Soil Heat Flux (W/m2)
%Calibrated parameter ranges ------TBD
%Amin - # to # %Minimum Albedo
%Aadd - # to # %Additive Albedo
%DPp - # to # %Decline parameter (Positive temperatures)
%DPn - # to # %Decline parameter (negative temperatures)
%SS - # to # %Significant snowfall (mm/h)
%Tp - # to # %Phase transition Temperature (K)
%Se - # to # %Snow Emissivity
%SHF - # to # %Soil Heat Flux (W/m2)
%PARAMETERS (used default)
SHCW=4180.0; %Specific heat capacity of water (J/(kgK))
SHCS=2100.0; %Specific heat capacity of snow (J/(kgK))
MH=337500.0; %Melting heat (J/kg)
SBC=0.0000000567; %Stefan-Boltzmann-Constant (W/(m2K^4))
CSH=2835500.0; %Condensation/Sublimation heat (J/kg)
%Preallocation
Albedo(1)=0;
SnowAge(1)=0;
ModeledSWE(1)=0;
Melt(1)=0;
Sublim(1)=0;
VapPresS(1)=0;
Pr(1)=0;
Ps(1)=0;
Z(1)=0;
SurfTemp(1)=0;
VapPresA(1)=0;
ShortRadBal(1)=0;
LongRadBal(1)=0;
SenHeatFlux(1)=0;
LatHeatFlux(1)=0;
AdvFluxR(1)=0;
AdvFluxS(1)=0;
%Precipitation Loop
for t=2 : length(P); %time series loop
if T(t)>=Tp; %Precip that is rain
Pr(t)=P(t);
else
Pr(t)=0;
end
if T(t)<Tp; %Precip that is snow
Ps(t)=P(t);
else
Ps(t)=0;
end
if Ps(t)>SS; %Snow age
SnowAge(t)=0;
else
if ModeledSWE(t-1)>0;
SnowAge(t)=SnowAge(t-1)+0.04167;
else
SnowAge(t)=0;
end
end
if T(t)>273.16;
Z(t)=DPp;
else
Z(t)=DPn;
end
if Ps(t)>SS; %Albedo
Albedo(t)=Amin+Aadd;
else
if SnowAge(t)>0;
Albedo(t)=Amin+(Albedo(t-1)-Amin)*exp(Z(t)*0.04167);
else
Albedo(t)= 0;
end
end
SurfTemp(t)=min(T(t),273.16);
VapPresA(t)=6.1078*exp((17.08085*(T(t)-273.16))/(234.175+(T(t)-273.16)))*(Relhum(t)/100);
if ModeledSWE(t-1)>0;
VapPresS(t)=6.1078*exp((17.088085*(SurfTemp(t)-273.16))/(234.175+(SurfTemp(t)-273.16)));
else
VapPresS(t)=0;
end
ShortRadBal(t)=(1- Albedo(t))*GlobalRad(t);
LongRadBal(t)=Inlong(t)-(Se*SBC*SurfTemp(t)^4);
SenHeatFlux(t)=18.85*(0.18+0.098*Wind(t))*(T(t)-SurfTemp(t));
LatHeatFlux(t)=32.82*(0.18+0.098*Wind(t))*(VapPresA(t)-VapPresS(t));
if Pr(t)>0;
AdvFluxR(t)=SHCW*(T(t)-273.16)*Pr(t)/3600;
else
AdvFluxR(t)=0;
end
if Ps(t)>0;
AdvFluxS(t)=SHCS*(T(t)-273.16)*Ps(t)/3600;
else
AdvFluxS(t)=0;
end
EB(t)=ShortRadBal(t)+LongRadBal(t)+SenHeatFlux(t)+LatHeatFlux(t)+AdvFluxR(t)+AdvFluxS(t)+SHF;
if ModeledSWE(t-1)>0;
Sublim(t)=3600*LatHeatFlux(t)/CSH;
else
Sublim(t)=0;
end
if T(t)>273.16;
if ModeledSWE(t-1)>0 && EB(t)>0;
Melt(t)= min(EB(t)*3600/MH ,ModeledSWE(t-1));
else
Melt(t)=0;
end
else
Melt(t)=0;
end
if ModeledSWE(t-1)>0;
ModeledSWE(t)=max((ModeledSWE(t-1)+Pr(t)+Ps(t)+Sublim(t)-Melt(t)),0);
else
ModeledSWE(t)=max((Ps(t)+Sublim(t)-Melt(t)),0);
end
end
daynr=1:length(P);
dlmwrite('output.txt', transpose(ModeledSWE), 'newline','pc','delimiter', '\t')
quit force
0 Comments
Answers (1)
Star Strider
on 9 Feb 2016
You can force a column vector by adding an extra dimension to the subscripts so that it saves by rows instead of by the default columns:
for k1 = 1:10
row_vector(k1) = k1^2;
col_vector(k1,:) = k1^2;
end
0 Comments
See Also
Categories
Find more on Oceanography and Hydrology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!