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

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!!!

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 12월 23일
편집: Andrei Bobrov 2015년 12월 23일
a = fastTimes(:);
ii = find(diff(a)<0.815);
fastTimesChop = [a(ii),a(ii+1)];

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2015년 12월 23일

편집:

2015년 12월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by