extracting value from a vector

Rob Mahurin rob at utk.edu
Tue Apr 21 11:48:36 CDT 2009


On Tue, Apr 21, 2009 at 10:44 AM, Alain Baeckeroot  
<alain.baeckeroot at laposte.net> wrote:
> Hi
>
> A trivial question, but i don't find the answer in the doc.
> i would like to assign value of a vector individually to some  
> variables.
>  octave> v = [ 1.4 , 2.7, 3.1 ]
>
> This fails : octave> [ a, b, c ] = v;
> everything goes in 'a', and b is undefined.
>
> Is it necessary to write a = v(1); b = v(2) ...
> or did i missed something ?
>
> Alain

On Apr 21, 2009, at 11:02 AM, James Sherman Jr. wrote:
> Yes.  I may miss the subtleties, but the only way I've seen the  
> assignment
>
> [a,b,c] = <something>
>
> work is when <something> is a function and that it returns 3 different
> variables.


I want this often enough that I've written

function varargout = columns_of (in)
   [d1,d2] = size(in);
   varargout = mat2cell(in, d1, ones(1,d2) );
endfunction

so I can do e.g.

octave> v = rand(2,3), [a,b] = columns_of(v)
v =
    0.054753   0.509675   0.866045
    0.702775   0.483820   0.816340
a =
    0.054753
    0.702775
b =
    0.50968
    0.48382


Rob

-- 
Rob Mahurin
Department of Physics and Astronomy
University of Tennessee 		865 207 2594
Knoxville, TN 37996 			rob at utk.edu





More information about the Help-octave mailing list