MATLAB: Variable Variation over the same range

different rangingMATLABrangevariables

Hi everyone,
as a MatLab rookie I need your help! I´m working on a programm, where different variables shall assume certain values of a given range. I would like to achieve that these different variables assume values indipentently from each other even though the range is the same. This means, that I need to calculate all possible versions (i.e. where lam_o_p = 35, lam_i_p = 25 and lam_i_c = 40 or where lam_o_p = 30, lam_i_p = 30 and lam_i_c = 35).
%Span [m]
span=3.5:0.5:7;
%Number of lamellas [-]
lam=3:2:7;
%Depth outer lamella parallel to span [mm]
lam_o_p=20:5:50
%Depth inner lamella parallel to span [mm]
lam_i_p=20:5:50;
%Depth inner lamella cross to span [mm]
lam_i_c=20:5:50;
%Calculation of total depth [mm]
if(lam>5)
depth_tot_7=lam_o_p*4+lam_i_p*2+lam_i_c
else
0
I´m looking forward to your answers.
Thanks in advance

Best Answer

I am interpreting your question this way: you would like to compute depth_tot_7 for all possible combinations of the different values of lam_o_p, lam_i_p, and lam_i_c.
Here are two different methods.
Using ndgrid
lam_o_p=20:5:50;
lam_i_p=20:5:50;
lam_i_c=20:5:50;
[LOP,LIP,LIC] = ndgrid(lam_o_p,lam_i_p,lam_i_c);
size(LOP)
ans =
7 7 7
% Calculate total depth for all possible combinations of LOP, LIP, and
% LIC.
depth_tot_7 = LOP*4 + LIP*2 + LIC;
size(depth_tot_7)
ans =
7 7 7
Using implicit expansion
This method requires less memory and is usually faster than the ndgrid method.
lam_o_p = 20:5:50; % row vector
lam_i_p = (20:5:50)'; % column vector
lam_i_c = reshape(20:5:50,1,1,[]); % vector along 3rd dimension
depth_tot_7 = lam_o_p*4 + lam_i_p*2 + lam_i_c;
size(depth_tot_7)
ans =
7 7 7
For more information on MATLAB behavior when performing arithmetic on operands with different sizes, see "Compatible Array Sizes for Basic Operations."