Selecting values from a matrix via indices in another matrix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Selecting values from a matrix via indices in another matrix

Stefanie Hasler
Hello,

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!

Fii

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

James Sherman
On Tue, Sep 30, 2014 at 9:28 AM, Stefanie Hasler <[hidden email]> wrote:

> Hello,
>
> 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!
>
> Fii
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>

Hi Fii,

First, if your first matrix is 2x100, then it has 2 rows and 100
columns, so your first row would be 100 elements long.  Did you mean
that it is 100x2 or that your first column would be [20;200]?

If it is 2x100, and the rows are like you say they are, then one
(horribly inefficient) way is to do something like:
C = diag(B(A(:,1), A(:,2));

I'm sure there is a better ways, but this could be a start.

Hope this helps.

James Sherman

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Thomas Fehér
In reply to this post by Stefanie Hasler

On Tue, 30 Sep 2014 15:28:23 +0200
Stefanie Hasler <[hidden email]> wrote:

> Hello,
>
> 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!
>
> Fii
>

This should give the result that you want:

B(sub2ind(size(B),A(1,:),A(2,:)))

The trick is using the linear index of the matrix.

Hope that helps,

Thomas


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Juan Pablo Carbajal-2
In reply to this post by James Sherman


On Tue, Sep 30, 2014 at 6:57 PM, James Sherman Jr. <[hidden email]> wrote:
On Tue, Sep 30, 2014 at 9:28 AM, Stefanie Hasler <[hidden email]> wrote:
> Hello,
>
> 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!
>
> Fii
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>

Hi Fii,

First, if your first matrix is 2x100, then it has 2 rows and 100
columns, so your first row would be 100 elements long.  Did you mean
that it is 100x2 or that your first column would be [20;200]?

If it is 2x100, and the rows are like you say they are, then one
(horribly inefficient) way is to do something like:
C = diag(B(A(:,1), A(:,2));

I'm sure there is a better ways, but this could be a start.

Hope this helps.

James Sherman

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

This solution is also horrible but is easy to extend to more dimensions.

1. Convert you index matrix into a cell
subs_cell = mat2cell (A, size (A,1), ones (1,size (A,2)) );
2. Get the linear indeces from these subindeces
idx = sub2ind (size(B),subs_cell{:});
3. extract the data
C = B(idx);



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Markus Bergholz
In reply to this post by Stefanie Hasler


On Tue, Sep 30, 2014 at 3:28 PM, Stefanie Hasler <[hidden email]> wrote:
Hello,

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 =

                    32
                    27
                    74

octave:4> B(idx)
ans =

     0.169461945595782
     0.191624881686584
    0.0524569440834623

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



 

Fii

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave




--
icq: 167498924
XMPP|Jabber: [hidden email]

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Andrei Bobrov
In reply to this post by Juan Pablo Carbajal-2
subs_cell = num2cell(A,1);
idx = sub2ind (size(B),subs_cell{:});
C = B(idx);