Clear Filters
Clear Filters

Turn sequence into a loop

35 views (last 30 days)
Victor on 27 May 2024 at 14:14
Commented: Dyuman Joshi on 10 Jun 2024 at 5:00
idler_1 = 17;
idler_2 = 17;
idler_3 = 17;
Z_gears(1) = 17;
% Z_gear(1) is defined same holds for the idler's
% Number of Z_gears is determined by N and gear_ratio_combo goes from (1,1)
% to (1,(columlength)
% idlers are on Z_gear place Z_gear(3),Z_gear(5),Z_gear(7) etc
% but if Z_gear(end) is uneven then do not place idler there
Z_gears(2) = Z_gears(1)*gear_ratio_combo(1,1);
Z_gears(3) = idler_1;
Z_gears(4) = Z_gears(3)*gear_ratio_combo(1,2);
Z_gears(5) = idler_2;
Z_gears(6) = Z_gears(5)*gear_ratio_combo(1,3);
Z_gears(7) = idler_3;
Z_gears(8) = Z_gears(7)*gear_ratio_combo(1,4);

Answers (2)

Sanju on 27 May 2024 at 14:28
To turn the given sequence into a loop, you can use a for loop to iterate over the gear_ratio_combo and idler values. Here's an example of how you can modify the code,
  1. Initialize idler and the first Z_gears value.
  2. Determine the length of the gear_ratio_combo column.
  3. Manually set the second gear value.
  4. Use a loop to populate the rest of the Z_gears array,
  • If the index is even, assign idler.
  • If the index is odd, multiply the previous Z_gears value by the corresponding gear_ratio_combo.
idler = 17; % Since all idlers have the same value
Z_gears(1) = 17; % Initial gear value
% Define the number of gear ratios and Z_gears
column_length = size(gear_ratio_combo, 2);
Z_gears(2) = Z_gears(1) * gear_ratio_combo(1, 1);
% Loop to populate Z_gears
for i = 2:column_length
if mod(i, 2) == 0
Z_gears(i + 1) = idler;
Z_gears(i + 1) = Z_gears(i) * gear_ratio_combo(1, i);
Hope this helps!
Victor on 27 May 2024 at 17:03
Thank you for responding, However the Z_gear can run to N=8 for example while the column length of the gear_ratio_combo can also differ
Sanju on 28 May 2024 at 9:17
To handle varying lengths of gear_ratio_combo and Z_gears, you can use a nested for loop. Here's an updated code example,
idler_1 = 17;
idler_2 = 17;
idler_3 = 17;
Z_gears(1) = 17;
% Define gear_ratio_combo matrix
gear_ratio_combo = [1, 2, 3, 4];
% Preallocate Z_gears array
Z_gears = zeros(1, length(gear_ratio_combo)*2 + 1);
% Loop through gear_ratio_combo and idlers
for i = 1:length(gear_ratio_combo)
Z_gears(2*i) = Z_gears(2*i-1) * gear_ratio_combo(i);
if mod(Z_gears(2*i), 2) == 0
Z_gears(2*i+1) = idler_1;
% Additional loop for remaining idlers
for i = length(gear_ratio_combo)*2+2:length(Z_gears)
if mod(i, 2) == 0
Z_gears(i) = Z_gears(i-1) * gear_ratio_combo(i/2-1);
if mod(Z_gears(i), 2) == 0
Z_gears(i+1) = idler_1;
This updated code will handle cases where the length of gear_ratio_combo is different from the number of elements in Z_gears. The second loop will take care of adding the remaining idlers to Z_gears if necessary.

Sign in to comment.

Dyuman Joshi
Dyuman Joshi on 28 May 2024 at 9:54
A better approach would be to vectorize, see below. (I assume all the arrays to be used have compatible dimensions for operations performed)
Note that N must be equalto 2*columnlength.
N = 8;
%If the values for idlers are different then concatenate them in a
%1D array and assign to Z_gear afterwards
Z_gear(1:2:N) = idler;
Z_gear(2:2:N) = Z_gear(1:2:N).*gear_ratio_combo(1,:);
Though, I am not sure what you mean by this line -
"% but if Z_gear(end) is uneven then do not place idler there"




Community Treasure Hunt

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

Start Hunting!