필터 지우기
필터 지우기

Using i as an index within a loop in Matlab 2016a

조회 수: 1 (최근 30일)
Ken
Ken 2016년 10월 5일
댓글: Ken 2016년 10월 5일
Is there a simple, global way to force Matlab 2016 to allow i to be used as a vector index within a loop statement that uses i as the loop index? I've been using i as a convenient index in for loops forever, and the simple assignment statement in the for statement was always sufficient to convert it from it's built in complex type to an integer. i.e.
for i=1:5,
y(i)=stuff;
end;
In Matlab 2016a, this code barfs because i retains its complex definition within the scope of the loop, so can't be used as the index into a vector within the loop. Adding an explicit assignment to an integer e.g. i=0, before the loop, or changing i to another variable name works, but I'd rather not modify every routine that uses i as an index within a loop.
  댓글 수: 1
Steven Lord
Steven Lord 2016년 10월 5일
Please show the full, exact text of the error message you receive. The behavior you're describing shouldn't happen and doesn't happen when I use release R2016a or R2016b.
for i = 1:5
y(i) = 23;
end

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

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 10월 5일
I don't know what you mean by "this code barfs". I ran a simple for-loop in MATLAB R2016a with i as the index variable and experienced no barfing.
But, as an aside, because of the complex definition of i, I tend to prefer using ii instead of i in for-loops.
  댓글 수: 1
Ken
Ken 2016년 10월 5일
My mistake, sorry to waste your time. This issue was reported by a customer using our code, and I mis-interpreted the symptom working over the phone. Turns out the problem was another difference in later Matlab versions that we were already aware of. Thanks for the thoughtful reply.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by