For Loop to generate value alternately

14 ビュー (過去 30 日間)
Tommaso Donolato
Tommaso Donolato 2020 年 6 月 4 日
コメント済み: Tommaso Donolato 2020 年 6 月 4 日
Good morning,
I would like to ask you some help with writing a script. I have a beam with variable length. On this beam there are two types of load, whose values are 4 and 8. These two values alternate every 3 meters. I would like to write a script that for a number of times, starting from a fixed length, adding three meters for every loop, automatically it alternates the value of 4 and of 8.
For example starting from a beam of 12 meters, I have the loads which are 4, 8, 4 equidistant (3 meters from each other). I want to start a loop of 10 iteration, which adds 3 meter to the starting beam length (12 + 3) and add the load equal to 8 for the first iteration; for the second iteration it adds again 3 meters (12 + 3 + 3) and the added load is 4. These procedure should be performed n times.
Thanks a lot for your availability.

採用された回答

Paresh yeole
Paresh yeole 2020 年 6 月 4 日
% initialize n
length_of_beam = 12;
for i = 1 : n
length_of_beam = length_of_beam +3;
if mod(i,2) == 0
load(i) = 8;
else
load(i) = 4;
end
end
  5 件のコメント
Paresh yeole
Paresh yeole 2020 年 6 月 4 日
% initialize n
length_of_beam(1) = 12;
load(1) = 4;
for i = 2 : n
length_of_beam(i) = length_of_beam(i-1) +3;
if mod(i,2) == 0
load(i) = load(i-1) + 8;
else
load(i) = load(i-1) + 4;
end
end
% you have two 1 * n vectors length_of_beam and load
Tommaso Donolato
Tommaso Donolato 2020 年 6 月 4 日
Thank you very much, it works perfectly.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by