help-octave
[Top][All Lists]

## Re: extracting elements without 'for' loop

 From: Mike Miller Subject: Re: extracting elements without 'for' loop Date: Sun, 6 Jul 2003 20:25:22 -0500 (CDT)

```On Sun, 6 Jul 2003, Mike Miller wrote:

> I have an n-by-m matrix W that tells me which m elements to extract from
> each of the n rows of the n-by-2m matrix G.  So, if these were G and W:
>
> G=reshape([1:24],4,6); W=[1 3 6;2 3 5;1 4 5 ; 2 4 6];
>
> G =
>
>    1   5   9  13  17  21
>    2   6  10  14  18  22
>    3   7  11  15  19  23
>    4   8  12  16  20  24
>
>
> W =
>
>   1  3  6
>   2  3  5
>   1  4  5
>   2  4  6
>
>
> I would want to extract this matrix from G:
>
> G(1,W(1,1)) G(1,W(1,2)) G(1,W(1,3))
> G(2,W(2,1)) G(2,W(2,2)) G(2,W(2,3))
> G(3,W(3,1)) G(3,W(3,2)) G(3,W(3,3))
> G(4,W(4,1)) G(4,W(4,2)) G(4,W(4,3))

By George, I think I've got it:

do_fortran_indexing = 1;
output=G(n*(W-1)+1+[0:n-1]'*ones(1,m));

Sorry to answer my own question, but I had a sudden flash of insight.
Writing it all out probably helped.

Best,

Mike

--
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology
and Institute of Human Genetics
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/

-------------------------------------------------------------
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
-------------------------------------------------------------

```