Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract elements from a matrix using a vector of indices?

Suppose I have a matrix A of order m×n and a vector of order m×1. I would like to extract elements from each row of the matrix A by using the elements of the vector as an offset in each row.

For example,

A = [[3, 0, 0, 8, 3],
     [9, 3, 2, 2, 6],
     [5, 5, 4, 2, 8],
     [3, 8, 7, 1, 2],
     [3, 9, 1, 5, 5]]

and a vector

y = [4, 2, 1, 3, 2]

What I want to achieve is a way to extract the elements of A such that each element of the vector indexes an element in the corresponding row of A, i.e., implementing

for i in range(len(y)):
    A[i, y[i]] = #perform operations here

without the use of any explicit loops.

The expected output is,

[3, 2, 5, 1, 1]

I am using Python and the NumPy library.

like image 750
Suyash Shetty Avatar asked May 18 '16 05:05

Suyash Shetty


People also ask

How do you extract an element from a matrix?

The SAS/IML language supports two ways to extract elements: by using subscripts or by using indices. Use subscripts when you are extracting a rectangular portion of a matrix, such as a row, a column, or a submatrix. Use indices when you want to extract values from a non-rectangular pattern.

How do you access the matrix element in Python?

The data elements in a matrix can be accessed by using the indexes. The access method is same as the way data is accessed in Two dimensional array.

What is indexing of matrix?

Indexing into a matrix is a means of selecting a subset of elements from the matrix. MATLAB® has several indexing styles that are not only powerful and flexible, but also readable and expressive. Indexing is a key to the effectiveness of MATLAB at capturing matrix-oriented ideas in understandable computer programs.


1 Answers

You should start by converting list A into a NumPy array:

In [270]: import numpy as np

In [271]: A = np.array([[3, 0, 0, 8, 3],
     ...:               [9, 3, 2, 2, 6],
     ...:               [5, 5, 4, 2, 8],
     ...:               [3, 8, 7, 1, 2],
     ...:               [3, 9, 1, 5, 5]])

In [272]: cols = [4, 2, 1, 3, 2]

And after that, nothing prevents you from using advanced indexing:

In [273]: rows = np.arange(A.shape[0])

In [274]: rows
Out[274]: array([0, 1, 2, 3, 4])

In [275]: A[rows, cols]
Out[275]: array([3, 2, 5, 1, 1])

In [276]: A[rows, cols] = -99

In [277]: A
Out[277]: 
array([[  3,   0,   0,   8, -99],
       [  9,   3, -99,   2,   6],
       [  5, -99,   4,   2,   8],
       [  3,   8,   7, -99,   2],
       [  3,   9, -99,   5,   5]])
like image 196
Tonechas Avatar answered Oct 07 '22 09:10

Tonechas