Info

# Why does a for loop that has column vectors as input return row vectors as output

1 view (last 30 days)
Wesser on 6 Feb 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
% Water System Blending Model
% Written by
% Data for Well Pumpage and Metered Usage was provided by the Security Public Water System (PWS)
% Columns cited in notes are from the Model tab in Security_Blending_Data.xlsx
[Z1PumpTot]=xlsread('Security_Blending_Data.xlsx',1,"B3:B53"); %The "1" indicates the 1st excel tab shoudl be used in the work book
[SorW]=xlsread('Security_Blending_Data.xlsx',1,"F3:F53"); %Column is 0 or 1 to designate summer or winter
td = zeros(1,51); %Preallocation of voctor size to increase speed of computation
for t=1:length(td) %Loop to determine the Avg concnetration of PFHxS in Drinking WAter for each zone over time
Z1Conc(t)=Z1MassTot(t)/Z1PumpTot(t); %Total Mass PFHxS in Z1(Column 40)
Z3Conc(t)=Z3MassTot(t)/Z3PumpTot(t); %Total Mass PFHxS in Z3(Column 52)
TotPS(t)=Z1PumpTot(t)+Z3PumpTot(t); %Total Pumped Supply (Column 57)
if SorW(t)==0 %Z1 Percent Usage of Total Pumped Supply (Column 63)
Z1PerTotPS(t)=0.297430058;
else Z1PerTotPS(t)=0.361336585;
end
if SorW(t)==0 %Z2 Percent Usage of Total Pumped Supply (Column 64)
Z2PerTotPS(t)=0.367893872;
else Z2PerTotPS(t)=0.359354700;
end
if SorW(t)==0 %Z3 Percent Usage of Total Pumped Supply (Column 65)
Z3PerTotPS(t)=0.334676069;
else Z3PerTotPS(t)=0.279308716;
end
Z1F(t)=Z1PerTotPS(t)*TotPS(t); %Z1 Usage (based on % usage of TPS)(Column 66)
Z2F(t)=Z2PerTotPS(t)*TotPS(t); %Z2 Usage (based on % usage of TPS)(Column 67)
Z3F(t)=Z3PerTotPS(t)*TotPS(t); %Z3 Usage (based on % usage of TPS)(Column 68)
if Z1PumpTot(t)>Z1F(t) %Z1 to Z2 Flow (Column 69)
Z1toZ2F(t)=Z1PumpTot(t)-Z1F(t);
else Z1toZ2F(t)=0;
end
if Z3PumpTot(t)>Z3F(t) %Z3 to Z2 Flow (Column 72)
Z3toZ2F(t)=Z3PumpTot(t)-Z3F(t);
else Z3toZ2F(t)=0;
end
if Z1PumpTot(t)<Z1F(t) %Z2 to Z1 Flow (Column 70)
Z2toZ1F(t)=Z3toZ2F(t)-Z2F(t);
else Z2toZ1F(t)=0;
end
if Z3PumpTot(t)<Z3F(t) %Z2 to Z3 Flow (Column 71)
Z2toZ3F(t)=Z1toZ2F(t)-Z2F(t);
else Z2toZ3F(t)=0;
end
if Z1toZ2F(t)>0 %Z1 to Z2 Mass Load (Column 73)
Z1toZ2M(t)=Z1toZ2F(t)*Z1Conc(t);
else Z1toZ2M(t)=0;
end
if Z3toZ2F(t)>0 %Z3 to Z2 Mass Load (Column 74)
Z3toZ2M(t)=Z3toZ2F(t)*Z3Conc(t);
else Z3toZ2M(t)=0;
end
Z2TotM(t)=(Z1toZ2M(t)+Z3toZ2M(t))/(Z1toZ2F(t)+Z3toZ2F(t)); % Z2 Total Mass of PFHxS (Column 75)
if Z2TotM(t)>0 %Z2 to Z1 Mass Load (Column 76)
Z2toZ1M(t)=Z2TotM(t)*Z2toZ1F(t);
else Z2toZ1M(t)=0;
end
if Z2toZ3F(t)>0 %Z2 to Z3 Mass Load (Column 77)
Z2toZ3M(t)=Z2toZ3F(t)*Z2TotM(t);
else Z2toZ3M(t)=0;
end
Z1PFHxS(t)=(Z2toZ1M(t)+Z1MassTot(t))/(Z1PumpTot(t)+Z2toZ1F(t)); %Avg Concentration of PFHxS in Z1 (Column 78)
Z2PFHxS(t)=(Z1toZ2M(t)+Z3toZ2M(t))/(Z1toZ2F(t)+Z3toZ2F(t)); %Avg Concentration of PFHxS in Z2 (Column 79)
Z3PFHxS(t)=(Z2toZ3M(t)+Z3MassTot(t))/(Z2toZ3F(t)+Z3PumpTot(t)); %Avg Concentration of PFHxS in Z3 (Column 79)
end
SO, my question is, that I load column vectors from the excel sheet, and my loop returns row vectors....why? Does this matter?
Adam on 10 Feb 2020
I often forget which way round is default for an array, but you can easily test it on command line (where I test many many things out to enhance my understanding!) by just typing something like e.g.
a(20) = 1;
so long as the variable 'a' does not already exist. This will then automatically create an array of length 20, which will, by default, be a row, which you can see easily in the workspace. This is effectively the same as growing in a loop as it has to create the preceeding 19 elements (as zeros) in order to do what you ask it and make the 20th element of an array which does not yet exist a 1.