Is it possible to have two indices in a for loop?
2 views (last 30 days)
Show older comments
for i = [3 5 7 9 11 12 14 15 16 17]
for k = 1:length(pathwayEMG_HR)
for s = 2:6
time = AEMGstructHR(i,k).data.data(:,1);
AEMGtemp = AEMGstructHR(i,k).data.data;
Spier = AEMGtemp(:,s);
fs = 900;
N = 2;
fc1 = 5;
fc2 = 200;
[b,a] = butter(N,[fc1/(fs/2) fc2/(fs/2)]);
filtSpier = filtfilt(b,a,Spier);
if i == 11 && k == 1 && s == 2
figure()
plot(time, filtSpier);
xlabel('time [s]');
ylabel('EMG [V]');
title ('EMG TD11 HR1 Rectus Femoris')
end
end
end
Is it possible to have 2 indices in the same for loop? (i and k)
Because this code gives us errors al the time.
Thank you for helping!
1 Comment
Paul Hoffrichter
on 16 Dec 2020
>> Because this code gives us errors al the time.
I do not see how having two indices in a for-loop is going to solve whatever your error is. You should post the error and the line. In the Run button, check the Pause on Error to be able to examine the variables for the error line.
Answers (3)
Jan
on 16 Dec 2020
Edited: Jan
on 16 Dec 2020
iList = [3 5 7 9 11 12 14 15 16 17];
for = 1:length(pathwayEMG_HR)
i = iList(k);
...
end
By the wy: Move this expensive and constant part before the loop to save energy:
fs = 900;
N = 2;
fc1 = 5;
fc2 = 200;
[b,a] = butter(N,[fc1/(fs/2) fc2/(fs/2)]);
0 Comments
Paul Hoffrichter
on 16 Dec 2020
Jan's solution appears to not provide all combinations of (i,k) for the 2D array, AEMGstructHR. So I do not see how that construction is identical to the OP since most of the 2D array, AEMGstructHR, is not used.
iList = [3 5 7 9 11 12 14 15 16 17];
for k = 1:length(pathwayEMG_HR)
i = iList(k);
...
end
BTW, this saves even more energy by removing more items out of the innermost loop.
fs = 900;
N = 2;
fc1 = 5;
fc2 = 200;
[b,a] = butter(N,[fc1/(fs/2) fc2/(fs/2)]);
for i = [3 5 7 9 11 12 14 15 16 17]
for k = 1:length(pathwayEMG_HR)
time = AEMGstructHR(i,k).data.data(:,1);
AEMGtemp = AEMGstructHR(i,k).data.data;
for s = 2:6
Spier = AEMGtemp(:,s);
filtSpier = filtfilt(b,a,Spier);
if i == 11 && k == 1 && s == 2
figure()
plot(time, filtSpier);
xlabel('time [s]');
ylabel('EMG [V]');
title ('EMG TD11 HR1 Rectus Femoris')
end
end
end
end
1 Comment
Jan
on 17 Dec 2020
"Jan's solution appears to not provide all combinations of (i,k) for the 2D array" - exactly. This is my interpretation of "have 2 indices in the same for loop? (i and k)". "Two indices in the same loop" does not mean "all combinations". Otherwise two loops would be sufficient already.
Let's wait until the OP explains, what is meant or missing.
Paul Hoffrichter
on 16 Dec 2020
Edited: Paul Hoffrichter
on 16 Dec 2020
>> Is it possible to have 2 indices in the same for loop? (i and k).
In your i- and k-loops, you are considering every combination of i and k.
If so, to get all combinations of the i- and k-loops into one for-loop, first consider this double loop:
iList = [3 5 7 9 11 12 14 15 16 17];
kList = [2 6 8];
for i = 1:length(iList)
for k = 1:length(kList)
disp([iList(i) kList(k)])
end
end
Here is an equivalent single loop:
[m,n] = ndgrid(kList, iList);
Z = [ n(:), m(:)];
for ii = 1:length(Z(:,1))
i = Z(ii, 1);
k = Z(ii, 2);
disp([i k])
end
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!