필터 지우기
필터 지우기

if inside of for loop

조회 수: 1 (최근 30일)
Alejandro Estudillo
Alejandro Estudillo 2018년 2월 19일
댓글: Jan 2018년 2월 20일
I don't know what is wrong with this code. The code works when I don't include the if statement. Any idea?
for i = 1:3
if rt > 100 & rt < 1000
average(i) = mean(rt(cue==i))
dev(i) = std(rt(cue==i))
end
end
  댓글 수: 1
the cyclist
the cyclist 2018년 2월 19일
You are leaving too much for us to guess at, which makes it difficult for us to help you.
Please describe what you mean by "doesn't work" in more detail. Does the code give an error? If so, tell us the complete error message, and which line it occurs on.
If it does not give an error, but just an unexpected result, tell us the expected and actual result.
In general, it would be better to supply code that we can actually run ourselves (e.g. by supplying the values of rt and cue.)

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

채택된 답변

Jan
Jan 2018년 2월 19일
편집: Jan 2018년 2월 20일
Or perhaps:
match = (rt > 100 & rt < 1000);
rtx = rt(match);
cuex = cue(match);
for i = 1:3
average(i) = mean(rtx(cuex==i))
dev(i) = std(rtx(cuex==i))
end
Note that "if rt > 100 & rt < 1000" is evaluated as:
if all(rt > 100 & rt < 1000) && ~isempty(rt)
  댓글 수: 2
Alejandro Estudillo
Alejandro Estudillo 2018년 2월 20일
편집: Alejandro Estudillo 2018년 2월 20일
I just modified some bits and works pretty well!
match = (rt > 100 & rt < 1000);
rtx = rt(match);
cuex = cue(match);
for i = 1:3
average(i) = mean(rtx(cuex==i))
dev(i) = std(rtx(cuex==i))
end
Jan
Jan 2018년 2월 20일
@Alejandro: Yes, this way a typo. Inside the loop rtx and cuex are needed. I've fixed this in my answer.

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

추가 답변 (1개)

Birdman
Birdman 2018년 2월 19일
편집: Birdman 2018년 2월 19일
You probably forgot subscripting of rt.
for i = 1:3
if rt(i) > 100 & rt(i) < 1000
average(i) = mean(rt(cue==i))
dev(i) = std(rt(cue==i))
end
end
  댓글 수: 2
Alejandro Estudillo
Alejandro Estudillo 2018년 2월 20일
I tried the following code, but it only gives me a couple of values and the first one is 0.
for i = 1:3
if rt(cue==i) > 100 & rt(cue==i) < 1000
average(i) = mean(rt(cue==i))
dev(i) = std(rt(cue==i))
end
end
Birdman
Birdman 2018년 2월 20일
But again
rt(cue==i)
this will result in an array. You may perhaps do it before the loop and give it subscripted to for loop as
rt=rt(cue==i);
for i=1:3
if rt(i)...

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

카테고리

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