Questions tagged [matrix-indexing]

Indexing into a matrix is a means of selecting a subset of elements from the matrix/array

Matrix indexing is a way to reference a particular element in a matrix, by specifying its row and column number.

The notion extends to multi-dimensional arrays as well.

This is a fundamental concept in many languages such as MATLAB, Octave, Python+NumPy, R, Julia, etc...

There are several matrix-indexing methods:

  • row/column indexing: M(m,n) accesses the element on the m-th row on the n-th column.
  • linear indexing: this methods treats the matrix as a 1D list of elements and access the n-element. Note that there may be several ways of "flattening" the matrix row-first or column-first.
  • logical indexing: by specifying a mask

The use of indexing can often improve performance in the context of code , as it can replace for-loops, if conditions, etc.

302 questions
0
votes
1 answer

Using numpy, what is a good way to index into a matrix using another matrix whose entry values are column numbers?

Suppose I want to indepedently re-order each row of a matrix. Here is an example of that using np.argsort(): >>> A array([[88, 44, 77, 33, 77], [33, 55, 66, 88, 0], [88, 0, 0, 55, 88], [ 0, 22, 44, 88, 33], [33, 33,…
Tyler
  • 28,498
  • 11
  • 90
  • 106
0
votes
0 answers

Python implementation of Francis double step QR iteration algorithm does not converge

I am implementing the Francis double step QR Iteration algorithm using the notes and psuedocode from lecture https://people.inf.ethz.ch/arbenz/ewp/Lnotes/chapter4.pdf - Algorithm 4.5 The psuedocode is provided in Matlab I believe. Below is the…
0
votes
1 answer

Fastest way to write in an numpy array at specific indexes?

I would like to get the fastest solution to write data in a 2D numpy array using an array of indexes. I have a large 2D boolean numpy array buffer import numpy as np n_rows = 100000 n_cols = 250 shape_buf = (n_rows, n_cols) row_indexes =…
Wang Lee
  • 19
  • 4
0
votes
1 answer

How to access multiple elements in c++ Eigen array?

I want to retrieve certain elements in an Eigen array and return them as a vector. I use the following code: Eigen::ArrayXXi test; test.resize(5,5); test.setRandom(); Eigen::Matrix inds; inds<<0, 2, 3, 2, 3, 1; auto res =…
0
votes
2 answers

Getting a subset of numpy array indicies an easy way

Can this for loop be written in a simpler way? import itertools import numpy as np def f(a, b, c): # placeholder for a complex function print(a+b+c) a = np.arange(12).reshape(3, 4) for y, x in itertools.product(range(a.shape[0]-1),…
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
0
votes
1 answer

Indexing a 4D NumPy Array with two 2D arrays

I have a 4D target NumPy array which I want to fill with values from a 2D source array, using two additional 2D arrays which specify the position in the second and third axis of the target array where the value from the source array should be…
0
votes
1 answer

Equivalent of NumPy index arrays with standard Python lists or arrays

I can use an array or a list to index into numpy.array, e.g.: a = np.array([1, 2, 3, 4]) print(a[[1, 3]]) will produce [2 4] Is there an equivalent construct to index into a standard Python list or array? Just to be more specific: indexing has to…
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
0
votes
1 answer

Accessing an individual element in a Numpy array of arbitrary dimensions

So. I am trying to access and modify the penultimate individual element of a series of Numpy arrays. array_1D = np.array([10,11,12,13, 14]) array_2D = np.array([[20,30,40,50,60], [43,54,65,76,87], [11,22,33,44,55]]) array_3D =…
Gheko
  • 21
  • 3
0
votes
0 answers

Python Matrix indexes

I have a matrix with 3 dimension: import numpy as np a = np.array([[['Mon',18,20,22,17],['Tue',11,18,21,18]], [['Wed',15,21,20,19],['Thu',11,20,22,21]], [['Fri',18,17,23,22],['Sat',12,22,20,18]]])** and if I do print(a[0,1,3]) and…
0
votes
1 answer

How to Convert Matrix Indexing Code from MATLAB to R

I have a one-dimensional matrix called h1 and a 3-dimensional matrix called nv in my MATLAB code. I then run the code h1(nv) to create a new 3 column matrix hc. I need to convert this code into R. I am not sure how to do this sort of matrix indexing…
rose_t
  • 93
  • 5
0
votes
1 answer

numpy - why Z[(0,2)] can be view for some cases and be copy in the others?

Continuation to the question numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?. I got the answer and understood that comma triggering advanced index makes a totally different indexing. The answer also provided a way using __array_interface__…
mon
  • 18,789
  • 22
  • 112
  • 205
0
votes
1 answer

numpy - is Z[[1, 2],[1]] completely same with Z[(1,2),(1)]?

As per the answer of numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?, now understood comma can trigger advanced indexing which is also warned in the document. Warning The definition of advanced indexing means that x[(1,2,3),]…
mon
  • 18,789
  • 22
  • 112
  • 205
0
votes
1 answer

Multidimensional Indexing of an multidemensional matrix in Python

I want to use a multidimensional index matrix to access another multidimensional matrix. My problem is, that methods such as np.newaxis are not working because of broadcasting issues (mismatch of shapes). My data matrix has a shape of (5001, 3, 240,…
Bobbyyyyy
  • 3
  • 1
0
votes
1 answer

NumPy - convert indices to bools in one line

I have NumPy N-dimensional bool array b. It was converted to indices of True values by i = np.nonzero(b). What is the shortest one-liner to convert i back to b. Of cause it can be done multi-line as: b = np.zeros(b_shape, dtype = np.bool_) b[i] =…
Arty
  • 14,883
  • 6
  • 36
  • 69
0
votes
1 answer

Transfer python/numpy indexing to Tensorflow and improve performance

In an earlier question here, I asked for advice on faster item assignment to an array. Since then, I have done some progress, eg I expanded the recommended version to take care of 3-D arrays, which is intended to resemble the batch size of the later…
emil
  • 194
  • 1
  • 11