help-octave
[Top][All Lists]

## Re: vector handles

 From: James Sherman Jr. Subject: Re: vector handles Date: Fri, 12 Nov 2010 12:31:17 -0500

2010/11/12 Jordi Gutiérrez Hermoso
On 12 November 2010 10:00, george.brida <address@hidden> wrote:

> suppose that we have the following vector u=[10 11 12 14 15 14 10 11
> 12 14 15 17 20 21 20 23]' of length 16. I would like to create  the
> following vectors:

> u_1 composed by the elements number 1, 5, 9 and 13 of u
> u_2 composed by the elements number 2, 6, 10 and 14 of u
> u_3 composed by the elements number 3, 7, 11 and 15 of u
> u_4 composed by the elements number 4, 8, 12 and 16 of u

I think you want x = reshape(u,4,4) and then the rows of x will
contain the vectors you want.

- Jordi G. H.

You could also just access the elements directly if you want to:
u_1 = u(1:4:end);
u_2 = u(2:4:end);
and so on.
But Jordi is right, for this example, its probably more efficient to use reshape.

Hope this helps.

P.S. the "end" part of the command automagically calculates the length of the array that it appears in, so for this case, since the length of u is 16, its equivalent to writing, for example,
u_1 = u(1:4:16);