[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: extracting elements
From: |
Steve C. Thompson |
Subject: |
Re: extracting elements |
Date: |
Sat, 30 Apr 2005 22:03:21 -0700 |
User-agent: |
Mutt/1.3.28i |
Mike,
How about this:
I=[1,2,5,4]'; % desired rows
J=[1,3;2,4;1,4;2,3]; % desired columns
X=reshape([1:20],4,5)'; % matrix to pull elements from
X1=X';
[Jr,Jc]=size(J);
[X1r,X1c]=size(X1);
a=(I-1)*X1r*ones(1,Jc);
Y=X1(J+a)
Steve
On Apr 30 16:32PM, Mike Miller wrote:
> 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
> -------------------------------------------------------------
>
-------------------------------------------------------------
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
-------------------------------------------------------------
- Re: extracting elements,
Steve C. Thompson <=