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
1
vote
1 answer

Fill certain indices of a 3d numpy array

I have a list of indices likes this: selected_coords = [[1, 8, 30], [15, 4, 6] ,...] And a list of values like this: differences = [1, 5, 8, 2, ...] Both have 500 entries. Now I want to fill a 3d numpy array with these values on the right index.…
maxmijn
  • 337
  • 1
  • 3
  • 10
1
vote
1 answer

Efficiently getting i-th column of i-th 2D slice of 3D NumPy array, for all i

Suppose I have a NumPy array A of shape (N,N,N). From this, I form a 2D array B of shape (N,N) as follows: B = np.column_stack( tuple(A[i,:,i] for i in range(N)) ) In other words, for the i-th 2D slice of A, I take it's i-th column; I then stack…
Coffee_Table
  • 262
  • 1
  • 5
  • 14
1
vote
4 answers

Correspondence label and coordinates' points

How to obtain the coordinates of the first and the last appearances (under column-major ordering) of each label present in a matrix? Example of a label matrix (where labels are 1 to 4): L = [ 1 1 1 1 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0…
Mac.
  • 303
  • 1
  • 12
1
vote
1 answer

"Conversion to cell from double is not possible"

I am trying to find features of EEG signals using TQWT. For finding the features of many columns I tried following code, yet I am getting the error: Conversion to cell from double is not possible. The code is: for k = 1:9 filename =…
1
vote
2 answers

julia index matrix with vector

Suppose I have a 20-by-10 matrix m and a 20-by-1 vector v, where each element is an integer between 1 to 10. Is there smart indexing command something like m[:,v] that would give a vector, where each element i is element of m at the index [i,v[i]]?
aberdysh
  • 1,634
  • 2
  • 13
  • 34
1
vote
2 answers

Obtaining row indices of a matrix that satisfies a condition in Python

I have been trying to obtain all the row indices of the matrix (A) that contains the element nd. The size of A is 4M*4, it takes ~ 12 sec for this operation. Link to the file : data # Download the file named elementconnectivity before running this…
Mechanician
  • 525
  • 1
  • 6
  • 20
1
vote
1 answer

Multiple indices for numpy array: IndexError: failed to coerce slice entry of type numpy.ndarray to integer

Is there a way to do multiple indexing in a numpy array as described below? arr=np.array([55, 2, 3, 4, 5, 6, 7, 8, 9]) arr[np.arange(0,2):np.arange(5,7)] output: IndexError: too many indices for array Desired…
Nickpick
  • 6,163
  • 16
  • 65
  • 116
1
vote
2 answers

Creating a slice of a matrix from a vector in Numpy

Provided that I have a matrix A of size 5 by 4, also a vector b of length 5 whose element indicates how many values I need in the corresponding row of matrix A. That means each value in b is upper-bounded by the size of second dimension of A. My…
1
vote
2 answers

Extract elements of matrix from a starting element and size

Sorry about the bad title, I'm struggling to word this question well. Basically what I want to do is extract elements from a 2d matrix, from row by row, taking out a number of elements (N) starting at a particular column (k). In for loops, this…
James
  • 683
  • 9
  • 25
1
vote
2 answers

Python indirect indexing

This might be a super easy question if you know how to do it, but I just can't figure out the syntax: I have an array of 5x10 zeros: y1 = np.zeros((5,10)) and an array 5x1 of index: index=np.array([2,3,2,5,6]). For each row of y1, I would like to…
Chea
  • 15
  • 3
1
vote
1 answer

How to to 0/1 subsetting in Theano?

The objective is to obtain the subset of an array of elements through values provided in another array. import theano import theano.tensor as T a = T.vector('X', dtype='int64') b = T.vector('Y', dtype='int64') c = a[b] g = function([a,b],c) x =…
Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
1
vote
2 answers

How to address elements linearly in Python?

In Matlab it is possible to access elements of a matrix linearly: >> A=[1 2 3; 4 5 6] A = 1 2 3 4 5 6 >> A(1) ans = 1 >> A(2) ans = 4 >> A(3) ans = 2 Looks like Matlab does reshape matrix on the fly. Is it…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
1
vote
2 answers

Linear indexing of Matlab matrices in MEX file

I have a NxN symmetric matrix F of the following form F_11 F_12 F_13 ... F_1N F_21 ... F_31 . . . F_N1 F_N2 F_N3 ... F_NN with each submatrices F_IJ of size m x m. This matrix is created in MatLab, and will be used in a C-programm. So…
PhRo
  • 13
  • 3
1
vote
3 answers

How to access multiple elements of an array efficienty in C++?

This if my first post, I hope I'll meet the standards... I'm translating into c++ (at which I'm quite new) a program originally written in MATLAB for reasons of efficiency. The piece of code I am actually working on resumes accesses to various…
1
vote
0 answers

Matrix access by x/y coordinates without linear indexing and looping in Matlab/Octave

I'm operating on very big, 2D, sparse matrices in Octave. I have hit the linear indexing limit of 2^31 and need to go bigger. The problem is I have two same-sized vectors of X and Y coordinates and would like to modify respective points without a…
er453r
  • 211
  • 2
  • 11