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
10
votes
7 answers

Matlab index to logic indexing

I have given a list of indices, e.g. i = [3 5] and a vector v = 1:6. I need a function f which returns the logical map for the vector v given the indices i, e.g.: f(i, length(v)) = [0 0 1 0 1 0] Since I will call this function several million…
blubb
  • 9,510
  • 3
  • 40
  • 82
10
votes
2 answers

Indexing of unknown dimensional matrix

I have a non-fixed dimensional matrix M, from which I want to access a single element. The element's indices are contained in a vector J. So for example: M = rand(6,4,8,2); J = [5 2 7 1]; output = M(5,2,7,1) This time M has 4 dimensions, but this…
Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
9
votes
3 answers

List comprehension and logical indexing

Slowly transitioning from Matlab to Python... I have this list of the form list1 = [[1, 2, nan], [3, 7, 8], [1, 1, 1], [10, -1, nan]] and another list with the same number of items list2 = [1, 2, 3, 4] I'm trying to extract the elements of list1…
IMK
  • 195
  • 2
  • 7
7
votes
3 answers

Elegant way of adding columns on a specific position in a data frame

I have a data.frame with 3 cols: date, rate, price. I want to add columns that come from a matrix, after rate and before price. df = tibble('date' = c('01/01/2000', '02/01/2000', '03/01/2000'), 'rate' = c(7.50, 6.50, 5.54), 'price' =…
7
votes
2 answers

How do I use a different index for each row in a numpy array?

I have a NxM numpy array filled with zeros and a 1D numpy array of size N with random integers between 0 to M-1. As you can see the dimension of the array matches the number of rows in the matrix. Each element in the integer array means that at that…
Alejandro Sazo
  • 796
  • 15
  • 31
7
votes
4 answers

How can I insert the values from a vector into a matrix using column-major order?

I want to insert a set of n values expressed as a vector into a corresponding set of locations in a matrix. The real-world application involves inserting a set of n sea surface temperature values into an image of a region that is represented as a…
Fleetboat
  • 189
  • 7
7
votes
2 answers

Interpretation of negative index when subsetting a data.frame

I am very new to R and at times get stuck with the codes. I came across one of this code as below. What does -7 mean in the code below? round(cor(longley[,-7]),3) I understand: round for rounding, longley as data.frame, 3: digits for rounding,…
mani
  • 251
  • 2
  • 6
  • 9
7
votes
1 answer

Fast(er) way of indexing matrix in R

Foremost, I am looking for a fast(er) way of subsetting/indexing a matrix many, many times over: for (i in 1:99000) { subset.data <- data[index[, i], ] } Background: I'm implementing a sequential testing procedure involving the bootstrap in R.…
Niels
  • 81
  • 5
6
votes
2 answers

Create indexing array with 'end' before vector exists

I was just wondering if there is a way to index using end before knowing a vector's size? It should work for arrays with different sizes. Like this: subvector = (2:end) % illegal use of end A=[1 2 3]; B=[4 5 6 7]; A(subvector) % should be 2…
Finn
  • 2,333
  • 1
  • 10
  • 21
6
votes
2 answers

R indexing arrays. How to index 3 dimensional array by using a matrix for the 3rd dimension

I'm having a question about indexing 3 dim arrays. Say I have a 3 dimensional array x<- c(1:36) dim(x) <- c(3,4,3) Now I want to extract values out of this array according to a matrix holding the 3rd dimension indices for all [i,j] positions. y…
Atomhamster
  • 65
  • 1
  • 5
6
votes
1 answer

Indexing in Theano

How can I index a matrix in Theano by a vector of indices? More precisely, say that: v has type theano.tensor.vector (e.g. [0,2]) A has type theano.tensor.matrix (e.g. [[1,0,0], [0,1,0], [0,0,1]]) The desired result is [[1,0,0], [0,0,1]]. I…
John Jaques
  • 560
  • 4
  • 17
6
votes
3 answers

Getting a grid of a matrix via logical indexing in Numpy

I'm trying to rewrite a function using numpy which is originally in MATLAB. There's a logical indexing part which is as follows in MATLAB: X = reshape(1:16, 4, 4).'; idx = [true, false, false, true]; X(idx, idx) ans = 1 4 13 …
petrichor
  • 6,459
  • 4
  • 36
  • 48
5
votes
1 answer

Use a BitArray in Julia to filter rows of an array

I'd like to filter each row of my matrix a such that each row contains non-negative values. First, I tried this: julia> a = [-1 2 3; 4 5 6; -5 3 4; 3 5 5] 4×3 Matrix{Int64}: -1 2 3 4 5 6 -5 3 4 3 5 5 julia> # Desired Operation after…
Arthur Lin
  • 85
  • 4
5
votes
2 answers

Can I slice a numpy array using an array as indices?

I have 2 numpy arrays: a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) b = np.array([2, 1, 2]) I want to use b as starting indices into the columns of a and set all the values of a from those column indexes onwards to 0…
user1389840
  • 651
  • 9
  • 24
5
votes
1 answer

How to broadcast numpy indexing along batch dimensions?

For example, np.array([[1,2],[3,4]])[np.triu_indices(2)] has shape (3,), being a flattened list of the upper triangular entries. However, if I have a batch of 2x2 matrices: foo = np.repeat(np.array([[[1,2],[3,4]]]), 30, axis=0) and I want to…
1
2
3
20 21