help-octave
[Top][All Lists]

## Re: no way for simple matrix lookup ?

 From: Mike Miller Subject: Re: no way for simple matrix lookup ? Date: Wed, 16 Jun 2004 12:20:44 -0500 (CDT)

```On Wed, 16 Jun 2004, D. Goel wrote:

```
[1] Suppose A is a nxn matrix. And b and are vectors. How do i return elements of A specified by b and c?
```
i.e. Want to return a vector D, such that
D(i) = A(b(i), c(i))

Is the only way to do that a for loop?  Is that inefficient?
```
```
```
Do you mean that b and c contain integer values in the range between 1 and n? You want for b and c to specify the rows and columns of A to retain?
```
D = A(b,c)

```
And your variable D will be a matrix with size(D)=[length(b), length(c)]. You can transform it into a vector using D=D(:), or you can do this:
```
D = reshape(A(b, c), length(b)*length(c), 1)

You don't have to loop over 'i'.

Best,

Mike

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

```