help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

extracting elements


From: Mike Miller
Subject: extracting elements
Date: Sat, 30 Apr 2005 16:32:33 -0500 (CDT)

Suppose I have an N x M matrix X and I wish to extract particular elements from certain rows of X. I want to be able to provide a list of rows as a vector, and I want to provide the columns within rows as a matrix like this:

I = [1,2,5,4]';
J = [1,3;2,4;1,4;2,3]

  1  3
  2  4
  1  4
  2  3

So I want to extract rows 1,2,5,4 in that order, but I want to extract only columns 1,3 in row 1, columns 2,4 in row 2, columns 1,4 in row 5 and columns 2,3 in row 4.

If X looks like this...

X=reshape([1:20],4,5)'

   1   2   3   4
   5   6   7   8
   9  10  11  12
  13  14  15  16
  17  18  19  20

...I can get what I want by doing this:

for i=1:length(I), Y(i,:)=X(I(i),J(i,:)); end

Y =

   1   3
   6   8
  17  20
  14  15

But I think there must be a way to do this sort of thing without a for loop. Any suggestions? Thanks in advance.

Mike

--
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology and Community Health
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
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]