Clear Filters
Clear Filters

How do I create a for loop for the this example?

1 view (last 30 days)
accelX_cd1 = accelX(i_start_cd1:i_end_cd1);
accelY_cd1 = accelY(i_start_cd1:i_end_cd1);
accelZ_cd1 = accelZ(i_start_cd1:i_end_cd1);
accelX_cd2 = accelX (i_start_cd2:i_end_cd2);
accelY_cd2 = accelY (i_start_cd2:i_end_cd2);
accelZ_cd2 = accelZ (i_start_cd2:i_end_cd2);
accelX_cd3 = accelX (i_start_cd3:i_end_cd3);
accelY_cd3 = accelY (i_start_cd3:i_end_cd3);
accelZ_cd3 = accelZ (i_start_cd3:i_end_cd3);
accelX_cd4 = accelX (i_start_cd4:i_end_cd4);
accelY_cd4 = accelY (i_start_cd4:i_end_cd4);
accelZ_cd4 = accelZ (i_start_cd4:i_end_cd4);
accelX_cd5 = accelX (i_start_cd5:i_end_cd5);
accelY_cd5 = accelY (i_start_cd5:i_end_cd5);
accelZ_cd5 = accelZ (i_start_cd5:i_end_cd5);
accelX_cd6 = accelX (i_start_cd6:i_end_cd6);
accelY_cd6 = accelY (i_start_cd6:i_end_cd6);
accelZ_cd6 = accelZ (i_start_cd6:i_end_cd6);
  1 Comment
Ghazwan
Ghazwan on 15 Oct 2022
You need to arrange them in arrays, with the columns (or rows) being the variables you need to loop.

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Oct 2022
The solution is easy, if you do not hide the index in the name of the variable as in i_end_cd1, i_end_cd2, ... Use a vector instead and real indices: i_end_cd(1), i_end_cd(2), ...
  5 Comments
Jan
Jan on 17 Oct 2022
"I named these segments condition 1 (cd1), condition 2 (cd2), condition 3 (cd3), condition 4 (cd4), condition 5 (cd5) and condition 6 (cd6)" - this is the problem already. Use arrays instead: Cond(1), Cond(2), ... I avoid to use "cd" as name of a variable, because this collides with the cd() command.
If you work with arrays, running a loop is trivial:
for k = 1:6
disp(Cond(k)) % Or whatever
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!