help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Loop variables


From: Francesco Potortì
Subject: Re: Loop variables
Date: Fri, 20 Apr 2012 13:15:11 +0200

>I was not clear enough. A variable can be assigned using two syntax:
>- global: a = randn(10, 10). In this case, whichever previously stored in a
>is thrown away.
>- partial: a(indx, 1) = 3. In this case, every component not referenced by
>indx is kept from previous iteration, and that's the issue which plagued my
>script.
>
>The requested feature was thus to detect partial access to non-existing
>variables.

It appears to be an initialisation problem.  It is good programming
practice to initialize any variable before use.  In your second example,
you'd better zero out the matrix a before the first use:

a = zeros (R, C)

This preallocates the matrix, which depending on your problem may also
improve the speed.  If you do not know the values of R and C, you can
clear out the variable, or initialise it to an empty value:

a = [];

>Now wrapping the whole loop body into a function indeed ensures a fresh
>start at each iteration.

It's another way to initialise the variables: making them local.  Choice
depends mainly on ease of maintenance: choose the way that looks easier
to read and understand.

-- 
Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058 (op.2111)
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040  
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it


reply via email to

[Prev in Thread] Current Thread [Next in Thread]