help-octave
[Top][All Lists]

## How doyou vectorize pulling values out of a matrix?

 From: John W. Eaton Subject: How doyou vectorize pulling values out of a matrix? Date: Mon, 24 Jul 2006 20:40:35 -0400

```On 24-Jul-2006, Robert A. Macy wrote:

| Assume there is mymatrix [500,500] and I wish to retrieve
| 100 certain values from there.
|
| The specific indices are stored in two vectors Xindex and
| Yindex that are each [1,100]
|
| this for loop works...
| >> for i=1:100
| >> output(i)=mymatrix(Xindex(i),Yindex(i));
| >> endfor
|
| but this didn't...
| >> output(1,:)=mymatrix(Xindex(1,:),Yindex(1,:));
| everything matches, what's wrong?
|
| how do you vectorize this operation?

output = mymatrix (sub2ind (size (mymatrix), xindex, yindex));

| shouldn't matter but running octave 2.1.50a on Win98

It might matter in this case because sub2ind was added to Octave about
6 months after the 2.1.50 snapshot was made.  Since sub2ind is a .m
file, you might be able to grab a copy from a newer version of Octave
and use it with what you have.  OTOH, you might eventually want to
consider upgrading to something more modern.

jwe

```