フィルターのクリア

How do I shorten my code to prevent writing the same thing over and over?

1 回表示 (過去 30 日間)
Tom Ruopp
Tom Ruopp 2016 年 1 月 14 日
コメント済み: dpb 2016 年 1 月 14 日
code I am using is as follows:
for i=1:length(Rpeaks) %starts at first peak which is first whole step
%if i==1
if Rpeaks(i)-halfsegment<0
window=(1:Rpeaks(i)+halfsegment); %takes into account that the forces don't start a full halfsegment prior to first valley
step=GRFright(window); %assign the step the actual data values
normstep=(step./max(step)); %normalize the step
toeoff=find(normstep < min(normstep)+.05 , 1);
RTO(n)=toeoff+window(1);
heelstrike=find(normstep(toeoff:end) > min(normstep)+.05 , 1);% 1st zero location; syntax of ,1 returns first satisfying value
RHS(n)=heelstrike+toeoff+window(1);
elseif Rpeaks(i)+halfsegment>length(GRFright)
window=(Rpeaks(i)-halfsegment:size(GRFright));
step=GRFright(window); %assign the step the actual data values
normstep=(step./max(step)); %normalize the step
toeoff=find(normstep < min(normstep)+.05 , 1);
RTO(n)=toeoff+window(1);
heelstrike=find(normstep(toeoff:end) > min(normstep)+.05 , 1);% 1st zero location; syntax of ,1 returns first satisfying value
RHS(n)=heelstrike+toeoff+window(1);
else
window=(Rpeaks(i)-halfsegment:Rpeaks(i)+halfsegment); %find window of frames-100 added to catch full end of step
step=GRFright(window); %assign the step the actual data values
normstep=(step./max(step)); %normalize the step
toeoff=find(normstep < min(normstep)+.05 , 1);
RTO(n)=toeoff+window(1);
heelstrike=find(normstep(toeoff:end) > min(normstep)+.05 , 1);% 1st zero location; syntax of ,1 returns first satisfying value
RHS(n)=heelstrike+toeoff+window(1);
n=n+1;
end
end
within the for loop I have the same lines written three times after the "If statement" defining what the window should be. Is there a way to shorten the code so I only have to make one change for each condition when necessary? Thanks in advance.

採用された回答

dpb
dpb 2016 年 1 月 14 日
編集済み: dpb 2016 年 1 月 14 日
if Rpeaks(i)-halfsegment<0
window=(1:Rpeaks(i)+halfsegment); %takes into account that the forces don't start a full halfsegment prior to first valley
elseif Rpeaks(i)+halfsegment>length(GRFright)
window=(Rpeaks(i)-halfsegment:size(GRFright));
else
window=(Rpeaks(i)-halfsegment:Rpeaks(i)+halfsegment); %find window of frames-100 added to catch full end of step
end
step=GRFright(window); %assign the step the actual data values
normstep=(step./max(step)); %normalize the step
toeoff=find(normstep < min(normstep)+.05 , 1);
RTO(n)=toeoff+window(1);
heelstrike=find(normstep(toeoff:end) > min(normstep)+.05 , 1);% 1st zero location; syntax of ,1 returns first satisfying value
RHS(n)=heelstrike+toeoff+window(1);
n=n+1
The above presumes that the increment on n is missing inside the first two conditions; if not, you're overwriting the arrays until the else case is selected so might as well throw them away, anyway.
Might be more legible if used select case end construct rather than the if elseif else end but that's also somewhat in the eye of the beholder.
  2 件のコメント
Tom Ruopp
Tom Ruopp 2016 年 1 月 14 日
Thanks so much. Kind of embarrassingly easy fix. Ill look into the select case as well.
dpb
dpb 2016 年 1 月 14 日
Sometimes it takes a fresh eye. The key here is, of course, that what you do once you set the window is identical; it gets harder where there's some that is, but some that isn't. Then factoring can become quite a trick.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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