Path: news.mathworks.com!newsfeed-00.mathworks.com!lon-transit.news.telstra.net!lon-spool.news.telstra.net!pit-in1.telstra.net!news.telstra.net!news-south.connect.com.au!bunyip2.cc.uq.edu.au!dh177.physics.uq.edu.au!timo
From: Timo Nieminen <timo@physics.uq.edu.au>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to be a good Matlab programmer
Date: Mon, 21 Apr 2008 08:09:03 +1000
Organization: University of Queensland
Lines: 38
Message-ID: <Pine.LNX.4.50.0804210807080.8195-100000@localhost>
References: <d6f9728b-07d7-4ebc-8264-e2485cde6259@c65g2000hsa.googlegroups.com>
NNTP-Posting-Host: dh177.physics.uq.edu.au
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
X-Trace: air.soe.uq.edu.au 1208729165 24669 130.102.173.177 (20 Apr 2008 22:06:05 GMT)
X-Complaints-To: news@uq.edu.au
NNTP-Posting-Date: Sun, 20 Apr 2008 22:06:05 +0000 (UTC)
X-X-Sender: timo@localhost
In-Reply-To: <fuf1oc$dmn$1@fred.mathworks.com>
Xref: news.mathworks.com comp.soft-sys.matlab:464191



On Sun, 20 Apr 2008, Tim Davis wrote:

> Timo Nieminen <timo@physics.uq.edu.au> wrote in message 
> ...
> 
> > While your example above is short enough to not suffer, if
> "dims = ..." 
> > was a long way from the loop, and the loop was longer, the
> reader might 
> > well assume that "i" is an integer loop counter when
> reading the code. 
> > Apart from using i or j as counters being bad practice in
> matlab 
> > (sqrt(-1) and all that), "for dim = dims" is better for
> readability.
> 
> I have to disagree about the use of i and j.  One should
> never rely on i or j as the imaginary unit value, since they
> can be masked by variables.  Use 1i instead, which can never
> be overwritten by a variable.  It is always a constant equal
> to sqrt(-1).  The variables i and j are not always equal to
> sqrt(-1) and should thus not be relied upon.

Excellent advice for robust programming. Especially for scripts.

> The variables i and j have been used since time immemorial
> as matrix row and column indices.  Stick with standard
> mathematical notation and use them as such.
>
> I personally think that the choice made, a long while back,
> to use "i" as sqrt(-1) was a mistake.

Perhaps, but now we must live with it.

-- 
Timo Nieminen - Home page: http://www.physics.uq.edu.au/people/nieminen/
E-prints: http://eprint.uq.edu.au/view/person/Nieminen,_Timo_A..html
Shrine to Spirits: http://www.users.bigpond.com/timo_nieminen/spirits.html