フィルターのクリア

Create a single for loop for multiple conditions

1 回表示 (過去 30 日間)
DIP
DIP 2018 年 1 月 31 日
回答済み: A. Sawas 2018 年 2 月 1 日
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 件のコメント
DIP
DIP 2018 年 2 月 1 日
Moritz, RobF, the speeds are different, how can I input multiple conditions for speed and use only one for loop ?
Walter Roberson
Walter Roberson 2018 年 2 月 1 日
"the speeds are different"
Not in the code you posted.

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

回答 (1 件)

A. Sawas
A. Sawas 2018 年 2 月 1 日
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by