For-Loop Midpoint Help

5 views (last 30 days)
Nick
Nick on 3 Oct 2014
Commented: Joseph Cheng on 3 Oct 2014
I'm looking to make a for-loop that looks 50 indices ahead and 50 indices behind, but the code won't precede past the line: elseif sum(count(max(i-49,1)):i,length(count)) > 1, I think it has something to do with the max function, but can't seem to work around it. Any help would be appreciated. Count is already defined as a vector of zeros or ones previously in the script.
for i = 1:length(count);
if sum(count(i:min(i+49,length(count)))) > 1
count(i) = 50;
elseif sum(count(max(i-49,1)):i,length(count)) > 1
count(i) = 50;
else
count(i) = 0;
end
end

Accepted Answer

Joseph Cheng
Joseph Cheng on 3 Oct 2014
Edited: Joseph Cheng on 3 Oct 2014
since you want it to do both +50 and -50 you can't have the if/else structured that way. you'll have to do break it up. if you read about the if/elseif, it won't go into the elseif section if the first if is true
The statements after the elseif are executed if the expression is true and all the preceding IF and elseif expressions are false
taken from the help elseif documentation.
also looking at the count as you're modifying it wouldn't after your first count(i)=50 always cause it to be 50 from then on?
  3 Comments
Joseph Cheng
Joseph Cheng on 3 Oct 2014
to comment on your max statement you don't need the ,length(count) in that statement as you needed it in your min(). Also it's outside of the max function anyways.
Joseph Cheng
Joseph Cheng on 3 Oct 2014
also since you look to be doing a moving window sum. look at trying something like count2 = conv(count,ones(1,100),'valid'). which i believe will do a sliding window with length 100 where it'll add up all the items. with that you can put the threshold of if the sum is greater than X put those indexes as 50.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!