Create a single for loop for multiple conditions

Hi, I have a for loop with several calculations. There are three conditions for which the calculations are more or less the same. How can I formulate the code so that I have just one for loop for multiple conditions ? Ex. A same set of speed calculations for a boat a) Moving in still water b) Moving with the current c) Moving against the current

8 comentarios

Birdman
Birdman el 31 de En. de 2018
Why don't you share the code? How can we help you without seeing the code?
DIP
DIP el 31 de En. de 2018
Editada: DIP el 31 de En. de 2018
1. Still River
Boat_Speed=40;
for i=1:41
% Vehicle speed
VMPG(i) = Vehicle_Speed; % mph
Vmps(i) = Vehicle_Speed*0.44704; % m/s
% Rolling resistance force (N)
Fr = Croll*Vwt;
% Frontal area (m2)
Af = 0.84*Vht*Vwd;
% Air density (kg/m3)
rho = Pamb/R/Tamb; ... many more calculations
2. Downstream
Boat_Speed=40;
for i=1:41
% Vehicle speed
VMPG(i) = Vehicle_Speed; % mph
Vmps(i) = Vehicle_Speed*0.44704; % m/s
% Rolling resistance force (N)
Fr = Croll*Vwt;
% Frontal area (m2)
Af = 0.84*Vht*Vwd;
% Air density (kg/m3)
rho = Pamb/R/Tamb; ...
3. Upstream
Boat_Speed=40;
for i=1:41
% Vehicle speed
VMPG(i) = Vehicle_Speed; % mph
Vmps(i) = Vehicle_Speed*0.44704; % m/s
% Rolling resistance force (N)
Fr = Croll*Vwt;
% Frontal area (m2)
Af = 0.84*Vht*Vwd;
% Air density (kg/m3)
rho = Pamb/R/Tamb; ... many more calculations
Birdman
Birdman el 31 de En. de 2018
What is your condition in this cases?
DIP
DIP el 31 de En. de 2018
there are three cases , the boat speed in still water, moving downstream and moving upstream. The for loop for each case is almost the same. Can I combine the three cases in a single for loop ?
Moritz
Moritz el 31 de En. de 2018
I have to say I don't see any difference between the different cases. Can you pin point it to me? Anyhow, why don't you use one for loop and a switch case within for the differences in calculations (alternatively an if else structure as there are only 3 different cases). Does this help?
RobF
RobF el 31 de En. de 2018
I also have to say that your conditions (still river - downstream - upstream) don't seem to change anything concerning the for loop. Where the point where the condition gets relevant?
DIP
DIP el 1 de Feb. de 2018
Moritz, RobF, the speeds are different, how can I input multiple conditions for speed and use only one for loop ?
"the speeds are different"
Not in the code you posted.

Iniciar sesión para comentar.

Respuestas (1)

A. Sawas
A. Sawas el 1 de Feb. de 2018
I suggest you use switch statement like this:
Boat_Speed=40;
% set the following variable based on the current water conditions
water_condition = 'Still River';
for i=1:41
% your code before calculating the speed
% calculate vehicle speed at different water conditions
switch water_condition
case 'Still River'
% your code to calculate speed in still river conditions
case 'Downstream'
% your code to calculate speed in downstream conditions
case 'Upstream'
% your code to calculate speed in upstream conditions
otherwise
warning('Unexpected water condition.');
end
% your code after calculating the speed
end

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

DIP
el 31 de En. de 2018

Respondida:

el 1 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by