Using i and j as variables
이전 댓글 표시
Shadowing Matlab's built-in functions is without doubt a bad programming practice. Therefore TMW suggests not to use i and j as names of variables, see: techdoc: Avoid Using i and j for Variables.
Today MLint suggests to use 1i instead of i to get the imaginary value. But this should avoid conflicts suffiently already and the very common and frequent usage of i and j as variables looses its conflict potential.
Did you ever run into problems by using i as a variable? Do you know any public or private code, which relies on i to be the imaginary unit? If so, is it a script or is it encapsulated in a function, such that the user-land programs can still misuse i in the typical for loops?
댓글 수: 1
Stephen23
2015년 2월 5일
The use of i and j also comes up regularly in the questions posed here on MATLAB Central Answers. I started a discussion on how this should be dealt with in responses to questions.
채택된 답변
추가 답변 (2개)
Daniel Shub
2012년 8월 24일
4 개 추천
Did you ever run into problems by using i as a variable: Yes, that is how I learned not to use it as a counter.
Do you know any public or private code, which relies on i to be the imaginary unit: Much of my signal processing work uses i as the imaginary unit. I have been using 1i recently, but my old code still has i.
If so, is it a script or is it encapsulated in a function, such that the user-land programs can still misuse i in the typical for loops: As far as I know I only have one saved script, so it is all in functions.
In my own code I tend to use counters like icol and irow instead of the bare i and j. When it is not clear what I am looping over I tend to go with ii or cnt.
댓글 수: 7
Matt Fig
2012년 8월 24일
Jan, reaching way back in memory, I seem to recall someone (not 'someone'!) having a problem with 'i' and posting the code on the NG. Also, note the use of the variable 'sum' here today! That is probably the one I see most...
Oleg Komarov
2012년 8월 30일
iFile, iStr, iTemperature etc... make me think of Apple products which wouldn't make me want to look at your code ever, ever again! :)
Sean de Wolski
2012년 8월 30일
@Matt, I think size is the one I've seen the most. I try to always use sz or szx.
Jan
2012년 8월 30일
Walter Roberson
2012년 8월 30일
Apple products: iPhone, iPad, iPod...
Sean de Wolski
2012년 8월 30일
And the iPottie.
Jan
2012년 8월 30일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!