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

Finding index of element matching condition of matrix - Matlab

Given a matrix Z(i,j) such that it maps to two arrays X(i), and Y(j). I am trying to find elements of Z (and hence the corresponding X and Y) within a certain range. I am now doing the following using logical indexing. Given this example X = 1:5; …
Ash
  • 318
  • 3
  • 14
4
votes
1 answer

Matlab: Change elements in 3D array using given conditions

I have a 3 dimensional array (10x3x3) in Matlab and I want to change any value greater than 999 to Inf. However, I only want this to apply to (:,:,2:3) of this array. All the help I have found online seems to only apply to the whole array, or 1…
emmalgale
  • 289
  • 3
  • 14
4
votes
2 answers

Rows without repetitions - MATLAB

I have a matrix (4096x4) containing all possible combinations of four values taken from a pool of 8 numbers. ... 3 63 39 3 3 63 39 19 3 63 39 23 3 63 39 39 ... I am only interested in the rows of the matrix that…
Nolan Conaway
  • 2,639
  • 1
  • 26
  • 42
4
votes
1 answer

Python numpy array indexing. How is this working?

I came across this python code (which works) and to me it seems amazing. However, I am unable to figure out what this code is doing. To replicate it, I sort of wrote a test code: import numpy as np # Create a random array which represent the 6…
Luca
  • 10,458
  • 24
  • 107
  • 234
4
votes
2 answers

Voxel neighborhood indexing - Detecting "out of bounds" in 26 neighbor access with linear indexing

Well, I don't know how to describe my problem with a title, I hope the one I got is correct. I have a matrix (Min the example below) that is a 3D image, composed, in this case, by 11x11x11 voxels (I made it logical just for easiness, and size is…
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
4
votes
1 answer

how to extract a zero-dimensional slice from a 1-dimensional array in numpy

Is there a way to slice a zero-dimensional sub-array from a 1-dimensional array? For example, if I have a N-dimensional ndarray arr, arr[0] returns a (N-1)-dimensional ndarray. However, if I have a 1-dimensional ndarray x, x[0] doesn't return a…
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
4
votes
3 answers

Extract several columns from 3d matrix

I currently have an array A which has dimensions N x t x t. I want to create a 2D matrix, N x t, of the form: B = [ A[:,1,1] A[:,2,2],...,A[:,t,t]] Obviously, 2 ways I could do this are writing it out in full (impractical since t is large) and…
Greg
  • 161
  • 6
4
votes
1 answer

MATLAB accessing multiple elements in sparse matrix using row and column index vectors

I feel there should be an easy solution but I can't find it: I have the sparse matrices A B with the same dimension n*n. I want to create matrix C which copies values in A where B is non-zero. This is my approach: [r,c,v] = find(B); % now I'd like…
Lisa
  • 3,365
  • 3
  • 19
  • 30
4
votes
2 answers

Access matrix value using a vector of coordinates?

Let's say we have a vector: b = [3, 2, 1]; Let's say we also have matrix like this: A = ones([10 10 10]); I want to use vector b as a source of coordinates to assign values to matrix A. In this example it will be equivalent to: A(3, 2, 1) = 5; Is…
drsealks
  • 2,282
  • 1
  • 17
  • 34
3
votes
2 answers

Apply multiple masks at once to a Numpy array

Is there a way to apply multiple masks at once to a multi-dimensional Numpy array? For instance: X = np.arange(12).reshape(3, 4) # array([[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9, 10, 11]]) m0 = (X>0).all(axis=1) # array([False,…
Thrastylon
  • 853
  • 7
  • 20
3
votes
1 answer

Explanation of numpy indexing ndarray[(4, 2), (5, 3)]

Question Please help understand the design decision or rational of the Numpy indexing with tuple (i, j) into a ndarray. Background When the index is a single tuple (4, 2), then (i=row, j=column). shape = (6, 7) X = np.zeros(shape, dtype=int) X[(4,…
mon
  • 18,789
  • 22
  • 112
  • 205
3
votes
3 answers

Matlab Matrixaddress

During my course I came across that expression: A(:,end:-1:1) I have trouble to understand and read the morphemic structure of the 2nd Operand "end;-1;1" Lets take the example: A=[1 2 3; 4 5 6; 7 8 9] I am aware of: A(:).. Outputs [1 2 3; 4 5…
3
votes
3 answers

Creating Label vector using Indicator Matrix in Matlab

Given a binary matrix M of size n x k, i would like to create a vector Label of size n x 1 such that entry of Label should contain the concatenated column index of M where its values are 1 for eg: If the M Matrix is given as M = [ 0 0 1 1 0…
Learner
  • 962
  • 4
  • 15
  • 30
3
votes
1 answer

Boolean indexing array through array of boolean indexes without loop

I want to index an array with a boolean mask through multiple boolean arrays without a loop. This is what I want to achieve but without a loop and only with numpy. import numpy as np a = np.array([[0, 1],[2, 3]]) b = np.array([[[1, 0], [1, 0]], [[0,…
Spark Monkay
  • 422
  • 4
  • 18
3
votes
1 answer

How in numpy get elements of matrix between two indices arrays?

Let's say I have a matrix: >> a = np.arange(25).reshape(5, 5)` >> a [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] and two vectors of indices that define a span of matrix elements that I want to…
dankal444
  • 3,172
  • 1
  • 23
  • 35