How do I create a loop and put the results that satisfy the loop into a new variable?

1 view (last 30 days)
I am trying to create a loop that takes the difference between successive items in a vector, and if the difference is less than a value (0.1 in the example below), then put the item in a new vector, called fastTimesChop. I don't know what I am doing wrong, but I get either errors or values in fastTimesChop that are not right. Below is what I have done so far.
fastTimes=(0.033:rand:300);
fastTimesChop=[];
for j=1:(length(fastTimes-1))
if((fastTimes(j+1))-(fastTimes(j)))<0.815
fastTimesChop=[fastTimes(j); fastTimes(j+1)];
end
end
Thank you for any help!!!

Answers (1)

Andrei Bobrov
Andrei Bobrov on 23 Dec 2015
Edited: Andrei Bobrov on 23 Dec 2015
a = fastTimes(:);
ii = find(diff(a)<0.815);
fastTimesChop = [a(ii),a(ii+1)];

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!