help with nested loops error

조회 수: 4 (최근 30일)
Roger Vota
Roger Vota 2017년 3월 4일
답변: Guillaume 2017년 3월 7일
Somehow only first step in a loop is executed:
spiketime = (1:3:100);
stim = (1:10:100);
for i=1:size(stim);
for t = 1:size(spiketime);
if abs(spiketime(t)-stim(i)) < 3;
spiketime(t) = [];
end
end
end
What am I missing?
  댓글 수: 2
per isakson
per isakson 2017년 3월 4일
Replace
size(stim);
by
length(stim)
Roger Vota
Roger Vota 2017년 3월 7일
Thank you very much, it works now!

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2017년 3월 7일
"Thank you very much, it works now!"
Well, after a fashion, it will have got rid of one error, but the code definitively won't work as you want. Deleting elements while you're looping over them never works: your loop index will get out of sync with the content of the array.
In any case, a loop is not needed:
spiketime = 1:3:100; %() brackets unneeded, just add noise
stim = 1:10:100
%in R2016b:
spiketime(any(abs(spiketime - stim.') < 3)) = [];
%in versions prior to R2016b
spiketime(any(abs(bsxfun(@minus, spiketime, stim')) < 3)) = [];

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by