How do I loop on multiple indicies?

1 回表示 (過去 30 日間)
Peter Weiss
Peter Weiss 2017 年 9 月 5 日
コメント済み: Cam Salzberger 2017 年 9 月 6 日
I have the following code that performs the calculation, but I need to repeat that calculation for about 100 sets of input values. Specifically, I need to vary the values for paro, mmhgo, flowrate, sechii, and transit. I can use a for loop for one of the variables, but I do not know how to code for changing them all simultaneously. I imagine that the code could read one line (row) of input data from a file with each field (column) corresponding to one of my variables just mentioned. In other words, for one set of calculations the inputs would be paro=44.3, mmgho=0.022, flowrate=1.7e7, sechii=45, and transit=5. For another run, paro=49.27, mmhgo=0.09, flowrate=5.69e10, sechii=76, and transit=23. Any suggestions on how to do this are much appreciated.
mixingconst=4; %number of times in one day that a day's volume of water is homogenized
sechii=76; %in cm
flowrate=1.70E+10; %Flow rate in L day-1
paro=49.29; %surface mean monthly PAR value mol m-2 day-1
mmhgo=[0.09,0.022]; %initial MMHg concentration in ng L-1
transit=14*mixingconst; %transit days * the number of mixing times each day
kz=(1.44/(sechii/100))/10; %puts kz into units of 0.1 m-1
kd=0.011; %mean value from incubation experiments
depth=70; %this I hold constant
z = 1:depth; %each layer is 0.1 m
parz = paro*exp(-kz*z); %light penetration equation
layerconst=exp(-kd*parz); %the loss fraction at each layer
mulconstant = sum(layerconst) / depth; %the loss fraction for the whole water column
mmhgnew = mmhgo * cumprod(repmat(mulconstant, 1, transit)); %the progression of water column concentrations (cumulative product).
mmhgmnew=mmhgnew*(flowrate/1e9); %the progression of water column mass (cumulative product)
mmhgmo=mmhgo*(flowrate/1e9); %water column mass on day 0
mmhgmloss=mmhgmo-mmhgmnew(transit) %cumulative mass lost

採用された回答

Cam Salzberger
Cam Salzberger 2017 年 9 月 5 日
Hello Peter,
The way you mentioned, having all the constants in a big matrix and looping through just the rows, would work fine. Or you can just have each constant as a vector of the same length, and loop through the indices. Something like this:
% Define parameters
paro = [44.3 49.27]; % Or however many entries you want
mmhgo = [0.022 0.09];
flowrate = [1.7e7 5.69e10];
sechii = [45 76];
transit = [5 23];
% Preallocate output variable(s)
mmhgmloss = zeros(size(paro));
% Perform calculation for each parameter set
for k = 1:numel(paro)
kz=(1.44/(sechii(k)/100))/10;
%... perform calculation indexing into each parameter ...
mmhgmloss(k) = mmhgmo-mmhgmnew(transit(k));
end
Hope that helps give you a framework to get started.
-Cam
  4 件のコメント
Peter Weiss
Peter Weiss 2017 年 9 月 5 日
Thank you Walter. I assume that I can also output the values of mmhgmloss(k) to a matrix or table . I am looking for the command to do that.
Cam Salzberger
Cam Salzberger 2017 年 9 月 6 日
Well, the way it's set up in my code, it will be a matrix. In this case, I believed that you were only looping through a set of "paired" parameters, so there would only be a single array as output. If the value of mmhgmloss for each set of parameters is a vector though, then you would preallocate with something like:
mmhgmloss = zeros(size(paro,1),lengthOfMmhgmloss);
and you could assign each row with something like:
mmhgmloss(k,:) = mmhgmo-mmhgmnew(transit(k));
You could do a similar thing by preallocating the table and then assigning each value as you go. See the table function for creating them, and this post for a suggestion on prallocation.
-Cam

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by