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

Numpy 2D array indexing with indices out of bounds

I found a substantial bottleneck in the following code: def get_value(matrix, index): if (index[0] >= 0 and index[1] >= 0 and index[0] < matrix.shape[0] and index[1] < matrix.shape[1]): return matrix[index[0], index[1]] …
Ladislav Ondris
  • 1,246
  • 10
  • 19
0
votes
1 answer

Square brackets operator in Matlab without comma between its two values

I'm having a hard time figuring out what this code does, because googling square brackets doesn't yield appropriate results for the way the search engine works. id2 is a 1x265 array (so basically a 1d vector with 265 values) m is a 1x245 array (so…
claw91
  • 161
  • 4
  • 11
0
votes
1 answer

Numpy: Shape mismatch error when putting 3d matrix into 4d matrix using boolean array indexing- python

I have an error in my code and I have recreated it using the simplest example: import numpy as np zeros_4d = np.zeros((5,10,15,1)) zeros_3d = np.zeros((10,15,1)) ones_3d = zeros_3d + 1 bool_array = np.arange(0,15,1)>8 zeros_3d[:,9:,:] =…
0
votes
0 answers

Is there a better way to perform calculations using an array of indices on a numpy array?

I have a 2 x N numpy array of indices and a 1 x N array of values. The indices range from [0, 0] to [Xmax, Ymax]. I need to populate an Xmax x Ymax array with the sum of the values at that index. The following code is a simple example. import numpy…
Scott S.
  • 29
  • 4
0
votes
0 answers

Python Pandas - .isnull() does not work with full dataframe index

I do not understand why i cannot use the method .isnull() for a precise element in my dataframe. raw_limits['CIRL1_I[X]'] returns 0 1 1 -1 2 0,75 3 -0,75 4 NaN 5 NaN 6 …
0
votes
2 answers

Keeping track of optimal indices while reducing a matrix by 1x1 each iteration

SETUP I have an NxN diagonal matrix and I shrink the matrix by 1x1 in each iteration. indices = np.arange(0, np.size(n_n_matrix, 0)).tolist() for iter in range(0, N-K) opt_indices = criterion(n_n_matrix) lost_index = [i for i in indices if i…
0
votes
1 answer

MathNet.Numerics.LinearAlgebra - How to lookup multiple rows in Matrix

I'm trying to do a sparse matrix multiplication using MathDotNet Numerics. Input: Matrix mat // dense matrix of size n x k, where n is large and k ~ 10 int[] index // int[] of length l ~ 10, e.g. { 7, 13, 11, ... } So I'd like to…
Kris
  • 22,079
  • 3
  • 30
  • 35
0
votes
1 answer

How to do a simple ndarray indexing operation

I'm trying to use numpy's "indexing with an array" feature. Given this code: import numpy as np x = np.array([[10, 20, 30], [40, 50, 60]]) index = ??? print(x[index]) I want to print: [10, 50] That is, I'd like to find the correct shape array that…
0
votes
0 answers

How to assign data to a multi-dimensional numpy array?

I have a numpy array batch initialized as follows: batch = np.zeros((50, 60, 1920, 1080, 3)) Its supposed to be an array of 50 different, 60FPS videos of dimension 1920x1080, and the 3 represents three channels - red, green, blue. Each video is…
PPrasai
  • 1,186
  • 13
  • 36
0
votes
1 answer

Cycle to retrieve elements under the matrix diagonal using Python Numpy with array indexing

within my Pyhton code, with the variable i I denote the row of the matrix A. In every step, I want to retrieve all the elements before the diagonal current element A[i,i]. this is the code I used through array indexing : import numpy as np A =…
JB-Franco
  • 256
  • 1
  • 3
  • 17
0
votes
4 answers

Selecting elements from a vector based on condition on another vector

I want to know how to select those numbers which correspond (i.e. same position) to my pre-defined numbers. For example, I have these vectors: a = [ 1 0.1 2 3 0.1 0.5 4 0.1]; b = [100 200 300 400 500 600 700 800] I need to select elements…
Ralden123
  • 19
  • 7
0
votes
1 answer

Numpy index filtering changing multiple variables

I notice unexpected results when applying index filtering to a numpy array (b[b < 3] = 0 ). Any variable that has been assigned from or to the variable that is being filtered will have the same filter applied i.e. if b = a, a will be filtered the…
0
votes
1 answer

Issue with understanding numpy array slicing

When slicing a Numpy array, it looks inconsistent to me. In[87]: y Out[87]: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) In[88]: y[0,0] Out[88]: 1 y[0,0] is 1. That's OK, but when I type In[89]: y[0,0:1] Out[89]: array([1]) Why is the…
Jase
  • 3
  • 1
0
votes
1 answer

Matlab: Sorting 2D matrix and retaining nodes in triangle groups

I am trying to optimize a Matlab script (below) that finds the bounding boxes to functional values of all lower left triangles in a 2D space. The code goes through all the triangles and then sorts the nodes in ascending order based on the functional…
Fredrik P
  • 682
  • 1
  • 8
  • 21
0
votes
1 answer

How to obtain an entry of a square matrix inside a non square matrix

I am trying to print the last element of the largest square sub-matrix of some arbitrary-shaped rectangular matrix. I have several hints for this task: Set the variable y to be the last diagonal entry of A. Since A may not be square, you will need…
i9-9980XE
  • 11
  • 5