Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
Wesser
Wesser le 6 Fév 2020
Clôturé : MATLAB Answer Bot le 20 Août 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
[td]=xlsread('Security_Blending_Data.xlsx',1,"A3:A53"); %year
[Z1PumpTot]=xlsread('Security_Blending_Data.xlsx',1,"B3:B53"); %The "1" indicates the 1st excel tab shoudl be used in the work book
[Z1MassTot]=xlsread('Security_Blending_Data.xlsx',1,"C3:C53");
[Z3PumpTot]=xlsread('Security_Blending_Data.xlsx',1,"D3:D53");
[Z3MassTot]=xlsread('Security_Blending_Data.xlsx',1,"E3:E53");
[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?
  3 commentaires
Wesser
Wesser le 10 Fév 2020
Thanks!
I wasn't aware of the following:
"What you don't seem to do is presize all the other arrays you create though so if they just grow in the loop they will by default be row vectors."
I converted the code to read the following and it works great! Thank you
[td]=xlsread('Security_Blending_Data.xlsx',1,"A3:A53");
[Z1PumpTot]=xlsread('Security_Blending_Data.xlsx',1,"B3:B53");
[Z1MassTot]=xlsread('Security_Blending_Data.xlsx',1,"C3:C53");
[Z3PumpTot]=xlsread('Security_Blending_Data.xlsx',1,"D3:D53");
[Z3MassTot]=xlsread('Security_Blending_Data.xlsx',1,"E3:E53");
[SorW]=xlsread('Security_Blending_Data.xlsx',1,"F3:F53");
[TimeXaxis]=xlsread('Security_Blending_Data.xlsx',1,"A3:A53");
Date=datetime(TimeXaxis,'ConvertFrom','excel');
%Preallocaiton of vector size to increase speed of computation
Z1Conc=zeros(51,1);
Z3Conc=zeros(51,1);
TotPS=zeros(51,1);
Z1PerTotPS=zeros(51,1);
Z2PerTotPS=zeros(51,1);
Z3PerTotPS=zeros(51,1);
Z1F=zeros(51,1);
Z2F=zeros(51,1);
Z3F=zeros(51,1);
Z1toZ2F=zeros(51,1);
Z3toZ2F=zeros(51,1);
Z2toZ3F=zeros(51,1);
Z2toZ1F=zeros(51,1);
Z1toZ2M=zeros(51,1);
Z3toZ2M=zeros(51,1);
Z2TotM=zeros(51,1);
Z2toZ1M=zeros(51,1);
Z2toZ3M=zeros(51,1);
Z1PFHxS=zeros(51,1);
Z2PFHxS=zeros(51,1);
Z3PFHxS=zeros(51,1);
Adam
Adam le 10 Fév 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.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by