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
5
votes
4 answers

Linear index of the maximum of a multi-dimensional matrix - MATLAB

Let's say I have a 3-dimensional matrix and have computed the max along the second dimension, and want to get the linear indices of the max values. However, the max-function only returns the subscripts along one dimension. A = randn([5,5,5]); …
dast
  • 53
  • 4
5
votes
3 answers

Subsref with cells

This issue appeared when I was answering this question. It should be some stupid error I am doing, but I can't get what error it is… myMatrix = [22 33; 44 55] Returns: >> subsref(myMatrix, struct('type','()','subs',{{[1 2]}} ) ); ans = 22…
Werner
  • 2,537
  • 1
  • 26
  • 38
5
votes
3 answers

Efficient method for finding elements in MATLAB matrix

I would like to know how can the bottleneck be treated in the given piece of code. %% Points is an Nx3 matrix having the coordinates of N points where N ~ 10^6 Z = points(:,3) listZ = (Z >= a & Z < b); % Bottleneck np = sum(listZ); % For later…
OrangeRind
  • 4,798
  • 13
  • 45
  • 57
5
votes
2 answers

Indexing of 2D array in matlab

I have a 6X4 matrix M1 containing only zeros. I also have two 1D arrays Y1 and Y2 each with length 4.The two arrays contain the desired index values. Now, I want to set(convert to 1) the elements of matrix M1 such that M1(Y1:Y2) is equal to 1 for…
5
votes
2 answers

Using a colon for indexing in matrices of unknown dimensions

When indexing matrices in MATLAB, can I specify only the first or last n dimensions, and have all others dimensions "selected automatically"? For example, I am writing a function which takes in an image, and displays it with imshow, which can either…
sebf
  • 2,831
  • 5
  • 32
  • 50
5
votes
2 answers

Summing array by indices from another cell array

I have an array: a = [109, 894, 566, 453, 342, 25] and another cell array of sub-indices of a, denoted as: subs = { [1,3,4], [2,5,6], [1,3], [3,4], [2,3,4], [6] }; I want to avoid the for-loop to calculate the following summations via…
John Smith
  • 617
  • 3
  • 16
4
votes
1 answer

numpy - how to combine multiple indices (replace multiple one-by-one matrix access with one access)

Update The implementation did not consider multiple occurrences of a same word, and self word occurrences. For instance when stride=2 and the word at the position is W, co-occurrence of X needs +2, self-co-occurrence of W needs…
mon
  • 18,789
  • 22
  • 112
  • 205
4
votes
3 answers

Splitting a vector into groups of a predefined size

I'm looking for a way to split an input vector into groups of a predefined size, with the last group being smaller if there is a remainder. I prefer the output as a cell, but I don't mind if it's any other class as long as it provides access to the…
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
4
votes
3 answers

Top K indices of a multi-dimensional tensor

I have a 2D tensor and I want to get the indices of the top k values. I know about pytorch's topk function. The problem with pytorch's topk function is, it computes the topk values over some dimension. I want to get topk values over both…
Mitiku
  • 5,337
  • 3
  • 18
  • 35
4
votes
2 answers

Cannot assign values to numpy array using 3D masking and indexing

I have an 3D array that is a mask. Additionally, I have some indices that encode where (array position) some values should be saved. Everything seems to be working fine, except that the output matrix is still empty after assigning the values to the…
seralouk
  • 30,938
  • 9
  • 118
  • 133
4
votes
3 answers

Numpy 3d array indexing

I have a 3d numpy array (n_samples x num_components x 2) in the example below n_samples = 5 and num_components = 7. I have another array (indices) which is the selected component for each sample which is of shape (n_samples,). I want to select from…
Ash
  • 3,428
  • 1
  • 34
  • 44
4
votes
2 answers

Vectorizing addition of subarray

Let's say I have two (large) vectors a=[0 0 0 0 0] and b=[1 2 3 4 5] of the same size and one index vector ind=[1 5 2 1] with values in {1,...,length(a)}. I would like to compute for k = 1:length(ind) a(ind(k)) = a(ind(k)) + b(ind(k)); end % a…
Julian
  • 189
  • 3
4
votes
1 answer

Inserting columns to a matrix in Matlab

I'd like to insert columns to a matrix, but the insertion column positions within the matrix differ by row. How can I do this without using for-loop? Following is a simplified example in MATLAB; From A,X,P, I want to get APX without using…
4
votes
3 answers

Matlab Convert Vector to Binary Matrix

I have a vector v of size (m,1) whose elements are integers picked from 1:n. I want to create a matrix M of size (m,n) whose elements M(i,j) are 1 if v(i) = j, and are 0 otherwise. I do not want to use loops, and would like to implement this as a…
gciriani
  • 611
  • 2
  • 7
  • 19
4
votes
2 answers

Indexing a 4D array using another array of 3D indices

A have a 4D array M (a x b x c x d) and an array I of indices (3 x f), e.g. I = np.array([1,2,3, ...], [2,1,3, ...], [4,1,6, ...]) I would like to use I to arrive at a matrix X that has f rows and d columns, where: X[0,:] = M[1,2,4,:] X[1,:] =…
John Manak
  • 13,328
  • 29
  • 78
  • 119
1 2
3
20 21