Using i and j as variables

8 visualizaciones (últimos 30 días)
Jan
Jan el 24 de Ag. de 2012
Comentada: Stephen23 el 5 de Feb. de 2015
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 comentario
Stephen23
Stephen23 el 5 de Feb. de 2015
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.

Iniciar sesión para comentar.

Respuesta aceptada

Matt Fig
Matt Fig el 24 de Ag. de 2012
Yes, I ran into much trouble in my early days of MATLAB using i and j as indices in signal processing and data manipulation code. I was writing my first big MATLAB project for a physicist back when I was a graduate student. After debugging for quite some time, I began to figure out why all of his indices were double characters! I soon adopted his convention and always teach this when teaching programming to others. Not only does the use of double characters (ii,jj,kk,mm,nn) prevent masking, but it makes them stand out as indices in the code - you just cannot miss what they are.
There was quite a bit of legacy MATLAB code that used i and j and globals (!) when I first began where I am. This often caused problems and bugs so I went through and turned all i's into ii's and all globals into locals. No bugs since. This was a mix of scripts and functions.

Más respuestas (2)

Daniel Shub
Daniel Shub el 24 de Ag. de 2012
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.
  1 comentario
Jan
Jan el 30 de Ag. de 2012
Worth to be accepted also. Thanks.

Iniciar sesión para comentar.


Jan
Jan el 24 de Ag. de 2012
Editada: Jan el 24 de Ag. de 2012
I personally prefer counters like iFile, iStr, iCol etc. For processing a matrix, why should I use i and j, when I can use iTemperature and iPressure?! Then checking the correct usage in the formulas is much easier.
In consequence I never had conflicts with i personally. I've seen problems by users, who shadow max and sum frequently, length sometimes and input in a few cases. But I do not remember any i related problems in codes posted by users in the Matlab forums. Finally I've used i in abstract codes posted in this forum. Nevertheless, I'll profit from Daniel's and Matt's real-world examples -- thanks!
[EDITED after Matt's comment] Of course sum also! How could I forget that?!
  7 comentarios
Sean de Wolski
Sean de Wolski el 30 de Ag. de 2012
And the iPottie.
Jan
Jan el 30 de Ag. de 2012
Ah, Walter, thanks. So "apple" does not mean the fruit, which accompanies the mankind for thousands of years, but the new company from America, which forbid my neighbors to sell the eiPott.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by