[Top][All Lists]

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

Re: Selecting values from a matrix via indices in another matrix

From: Markus Bergholz
Subject: Re: Selecting values from a matrix via indices in another matrix
Date: Tue, 30 Sep 2014 21:06:04 +0200

On Tue, Sep 30, 2014 at 3:28 PM, Stefanie Hasler <address@hidden> wrote:

I have two matrices: One 2x100 matrix A containing rows like [20 200], and one 300x300 matrix B containing a range of values from 0 to 20. 

Is there an easy way to extract a subset of the data contained in B as a vector by using the values in A as indices? For example, if A(1,:) says (20,20), the vector's first entry should be B(20,20).

Right now I'm using a for-loop, looping through A, and writing the values I extract from B into a new vector. It works, but it's not pretty or efficient.

Basically, I'd like to be able to write something like B(A), but that does not yield the correct output. 

Any help would be very appreciated!

What about this?

octave:1> B=rand(10,10);
octave:2> A=[2,4;7,3;4,8]
A =

                     2                     4
                     7                     3
                     4                     8

octave:3> idx = sub2ind(size(B), A(:,1), A(:,2))
idx =


octave:4> B(idx)
ans =


octave:5> B(2,4)
ans =    0.169461945595782
octave:6> B(A(1,1),A(1,2))
ans =    0.169461945595782



Help-octave mailing list

icq: 167498924
XMPP|Jabber: address@hidden

reply via email to

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