help-octave
[Top][All Lists]
Advanced

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

Re: function applied to each component in an if clause


From: John W. Eaton
Subject: Re: function applied to each component in an if clause
Date: Wed, 12 Mar 2008 12:22:38 -0400

On 12-Mar-2008, Olaf Till wrote:

| On Wed, Mar 12, 2008 at 07:27:50AM +0100, Thomas Weber wrote:
| > On 11/03/08 02:54 -0700, sururi wrote:
| > > 
| > > I'm wondering if there is a practical way of applying a comparison 
operator
| > > to each component of a vector..
| > 
| > Use indices:
| > 
| > x = [0.5, 1, 2]
| > lower = (x < 2)
| > x(lower) = 4*x(lower)
| > 
| > In case someone else reads this: the last line above prints out all
| > (three) elements of 'x'. I expected only the two matching elements of x.
| > Is my expectation wrong?
| Octave and matlab print out the contents of the whole variable to
| which is assigned (without ;), even if you assign only to some
| components. mmh --- this seams really not intuitive since in octave
| the assignment itself has a value, which comprises only the assigned
| components; if you type
| 
| a = x(lower) = 4*x(lower)
| 
| only the assigned components will be displayed.

In this case, the variable A contains only the subset of elements, so
only those are displayed.

In Matlab, assigment is not an expression, so this "problem" doesn't
appear.  OTOH, you can't use assignment as an expression either.

Also, wouldn't it be equally confusing if an indexed assignment
expression printed the subset of elements?  Or worse, if Octave
printed the subset of elements but Matlab printed the whole array (I'm
pretty sure we would hear some complaints about that...)?

jwe


reply via email to

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