Simplification of creating new variables in a loop from preset array names
Show older comments
I am woking on sensetivity analysis in Matlab and have to calculate sensetivity for every input parameter I have. Currently there is 10+ parameters inside so hardcoding all parameters by hand becomes waste of time. Thought, all calculations are exactly the same and I just need to provide different names to every variable.
Is there any solution to give the names within the for loop so it will automatially assign new name while itteration.
Here is a current view of the code:
%-------------------------------------------------------------------------
% Evaluate the first-order sensitivity r_Percentage_Internal_Wall
% sort according to Percentage_Internal_Wall values:
[r_Percentage_Internal_Wall_sort,I_r_Percentage_Internal_Wall] = sort(r_Percentage_Internal_Wall);
% evaluate the mean values of blocks of 100 samples:
r_Percentage_Internal_Wall_blocks = mean(reshape(r_Percentage_Internal_Wall_sort,n_MCS/100,100),1);
r_grey_energy_gwp_blocks_r_Percentage_Internal_Wall = mean(reshape(r_grey_energy_gwp(I_r_Percentage_Internal_Wall),n_MCS/100,100),1);
% these are the means of blocks of 100 samples of r_grey_energy_gwp sorted according to r_Const_Thickness values
% Estimate the first-order sensitivity values:
% Form is: VarY_X1=var(Y_blocks_X1); % the variance of E[Y|X1]
Var_r_grey_energy_gwp_r_Percentage_Internal_Wall = var(r_grey_energy_gwp_blocks_r_Percentage_Internal_Wall);
% First-order sensitivity indexes:
S_r_Percentage_Internal_Wall = Var_r_grey_energy_gwp_r_Percentage_Internal_Wall/r_grey_energy_gwp_var %#ok<NOPTS>
%-------------------------------------------------------------------------
% Evaluate the first-order sensitivity r_Reinforcement
% sort according to Reinforcement values:
[r_Reinforcement_sort,I_r_Reinforcement] = sort(r_Reinforcement);
% evaluate the mean values of blocks of 100 samples:
r_Reinforcement_blocks = mean(reshape(r_Reinforcement_sort,n_MCS/100,100),1);
r_grey_energy_gwp_blocks_r_Reinforcement = mean(reshape(r_grey_energy_gwp(I_r_Reinforcement),n_MCS/100,100),1);
% these are the means of blocks of 100 samples of r_grey_energy_gwp sorted according to r_Const_Thickness values
% Estimate the first-order sensitivity values:
% Form is: VarY_X1=var(Y_blocks_X1); % the variance of E[Y|X1]
Var_r_grey_energy_gwp_r_Reinforcement = var(r_grey_energy_gwp_blocks_r_Reinforcement);
% First-order sensitivity indexes:
S_r_Reinforcement = Var_r_grey_energy_gwp_r_Reinforcement/r_grey_energy_gwp_var %#ok<NOPTS>
I would like to do it in a for loop in such a way, that it automatically creates new variables and puts the name from array = ["Reinforcement","Percentage_Internal_Wall", ... ] and doing all the calculations presented above.
As well I have python code which was doing quite the same. Here it is:
for indi in indicators:
result_df['grey_energy_'+ indi] = interim_df['grey_energy_insul_' + indi] + interim_df['grey_energy_const_'+indi] + \
interim_df['grey_energy_window_' + indi] + interim_df['grey_energy_internal_' + indi]
result_df['grey_energy_D_'+ indi] = interim_df['grey_energy_insul_D_' + indi] + interim_df['grey_energy_const_D_'+indi] + \
interim_df['grey_energy_window_D_' + indi] + interim_df['grey_energy_internal_D_' + indi]
The structure I would like to have:
Indicators = {'r_Length' 'r_Height' and etc.}
for i = 1: indicators
% Evaluate the first-order sensitivity r_Reinforcement
% sort according to Reinforcement values:
[(Indicators{i})_sort,I_(Indicators{i})] = sort((Indicators{i}));
% evaluate the mean values of blocks of 100 samples:
(Indicators{i})_blocks = mean(reshape((Indicators{i})_sort,n_MCS/100,100),1);
r_grey_energy_gwp_blocks_(Indicators{i}) = mean(reshape(r_grey_energy_gwp(I_(Indicators{i})),n_MCS/100,100),1);
% these are the means of blocks of 100 samples of r_grey_energy_gwp sorted according to r_Const_Thickness values
% Estimate the first-order sensitivity values:
% Form is: VarY_X1=var(Y_blocks_X1); % the variance of E[Y|X1]
Var_r_grey_energy_gwp_(Indicators{i}) = var(r_grey_energy_gwp_blocks_(Indicators{i}));
% First-order sensitivity indexes:
S_(Indicators{i}) = Var_r_grey_energy_gwp_(Indicators{i})/r_grey_energy_gwp_var %#ok<NOPTS>
end
Accepted Answer
More Answers (0)
Categories
Find more on Call Python from MATLAB 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!