フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by