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

조회 수: 5 (최근 30일)
Alexa Z
Alexa Z 2020년 12월 16일
댓글: Jan 2020년 12월 17일
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