help-octave
[Top][All Lists]
Advanced

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

Re: Assigning to multiple scalars from a vector


From: Geordie McBain
Subject: Re: Assigning to multiple scalars from a vector
Date: Thu, 23 Dec 2004 08:41:03 -0500
User-agent: Mutt/1.5.6+20040907i

Neilen:

Given the function

  function varargout = multiassign (data)
    for k=1:nargout
      varargout{k} = data(:,k);
    endfor
  endfunction

you can do

  octave> vec = [1, 2, 3]; [A, B, C] = multiassign (vec)
  A = 1
  B = 2
  C = 3

which is pretty close to what you want.

See http://wiki.octave.org/wiki.pl?VariableLengthReturnLists

Geordie McBain
www.aeromech.usyd.edu.au/~mcbain

On Wed, Dec 22, 2004 at 06:20:14PM +0200, Neilen Marais wrote:
> Hi All.
> 
> I'd like to be able to assign to several scalar variables from a vector
> in one shot.  Say I have a vector like this :
> vec = [1 ; 2 ; 3]
> 
> I would like to be able to do something like this:
> 
> [A ; B ; C] = vec
> 
> and get A = 1, B = 2, C = 3.
> 
> Octave seems to consider the individual scalars as vectors, so there
> aren't enough elements on the RHS according to it. Is there a way to
> make this work, or will I have to manually say A = vec(1) etc... ?
> 
> Thanks
> Neilen
> 
> 
> 
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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