help-octave
[Top][All Lists]

## Re: extracting value from a vector

 From: Rob Mahurin Subject: Re: extracting value from a vector Date: Tue, 21 Apr 2009 12:48:36 -0400

On Tue, Apr 21, 2009 at 10:44 AM, Alain Baeckeroot <address@hidden> 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