For Loop to generate value alternately
Show older comments
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.
Accepted Answer
More Answers (1)
Ora Nito
on 7 Jun 2024
In vectorized form:
N = 10;% must be even
lob0 = 12;
dlob = 3;
length_of_beam_1 = lob0 - dlob + cumsum(dlob.*ones(1,N));
l1 = 4.*ones(1,fix(N/2));
l2 = 8.*ones(1,fix(N/2));
load_temp = [l1; l2];
load_1 = cumsum(load_temp(:))';
Categories
Find more on Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!