Variable Variation over the same range

2 次查看(过去 30 天)
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

采纳的回答

Steve Eddins
Steve Eddins 2020-10-23
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."
  2 个评论
Davide Bertoldi
Davide Bertoldi 2020-10-26
Hello Steve,
firstly I want to thank you for your rapid answer, but unfortunately due to my bad explanation it is not exactly what I need. So let me try again (see also the modified code below): if the first logical expression is correct, depth_tot_7 will be calculated. If not, the (if correct) next logical check calculates depth_tot_5 or depth_tot_3. What I want to achieve are the following two things:
a) I want to define a range from 3, 5, 7 for lam without having to specify one of these values at the beginning.
b) If lam=3, I would like the program to combine all of the possible lamella depths that are contained in the regarding formula. This means that I´d like my programm to create total depths where lamellas have different attributed depths (for lam = 3 i.e. 25*2+50, 35*2+20, 50*2+35, 45*2+50 etc.). I would like to achieve that the values are differently combined to each other, considering the fact that these single lamellas have the same range and steps.
I hope that my explanation is now better and looking forward to your answer and help.
Best regards
%Spannweite [m]
span=3.5:0.5:7;
%Number of lamellas [-]
lam=3
%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
elseif (lam>3)
depth_tot_5=lam_o_p*2+lam_i_p*1+lam_i_c
else
depth_tot_3=lam_o_p*2+lam_i_c
end
Steve Eddins
Steve Eddins 2020-10-27
The question is still not perfectly clear to me, but perhaps you could create a function that would take lam as the input argument and return depth_tot. It might look something like this:
function depth_tot = my_fcn(lam)
%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=lam_o_p*4+lam_i_p*2+lam_i_c
elseif (lam>3)
depth_tot=lam_o_p*2+lam_i_p*1+lam_i_c
else
depth_tot=lam_o_p*2+lam_i_c
end
Then you could call your function like this:
lam_range = 3:0.5:7;
for k = 1:length(lam_range)
D(k,:) = my_fcn(lam_range(k));
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by