Is it possible to have two indices in a for loop?

2 次查看(过去 30 天)
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 个评论
Paul Hoffrichter
Paul Hoffrichter 2020-12-16
>> 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.

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2020-12-16
编辑:Jan 2020-12-16
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)]);

Paul Hoffrichter
Paul Hoffrichter 2020-12-16
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 个评论
Jan
Jan 2020-12-17
"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
Paul Hoffrichter 2020-12-16
编辑:Paul Hoffrichter 2020-12-16
>> 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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by