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
46
votes
3 answers

What does [:, :] mean on NumPy arrays

Sorry for the stupid question. I'm programming in PHP but found some nice code in Python and want to "recreate" it in PHP. But I'm quite frustrated about the line: self.h = -0.1 self.activity = numpy.zeros((512, 512)) + self.h self.activity[:,…
user2432721
  • 465
  • 1
  • 4
  • 6
44
votes
7 answers

How can I find the maximum value and its index in array in MATLAB?

Suppose I have an array, a = [2 5 4 7]. What is the function returning the maximum value and its index? For example, in my case that function should return 7 as the maximum value and 4 as the index.
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
36
votes
1 answer

Linear indexing, logical indexing, and all that

We are used to different forms of indexing in Matlab: standard (using integers along each dimension), logical (using logical values), linear (using a single index to traverse an array with more than one dimension). At first sight, it may appear…
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
31
votes
1 answer

Does Matlab accept non-integer indices?

Of course not! ...Or does it? Let's do some tests. Define x = [10 20 30 40 50]. Then any of the following statements, as expected, gives an error in Matlab (Subscript indices must either be real positive integers or logicals): >> x(1.2) >>…
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
28
votes
1 answer

How do I remove elements at a set of indices in a vector in MATLAB?

I have a vector with 100 elements. I have another vector with index positions of elements I want to remove from this vector. How do I do this?
Ted Flethuseo
  • 385
  • 2
  • 5
  • 7
22
votes
3 answers

Can I slice tensors with logical indexing or lists of indices?

I'm trying to slice a PyTorch tensor using a logical index on the columns. I want the columns that correspond to a 1 value in the index vector. Both slicing and logical indexing are possible, but are they possible together? If so, how? My attempt…
Cecilia
  • 4,512
  • 3
  • 32
  • 75
19
votes
3 answers

In R, what does a negative index do?

I am porting part of a program (not enough to compile and run) from R to C++. I am not familiar with R. I have done okay using the references online, but was stumped by the following line: cnt2.2<-cnt2[,-1] I am guessing: cnt2 is a 2 dimensional…
Brad
  • 1,360
  • 4
  • 18
  • 27
17
votes
3 answers

How to select a submatrix (not in any particular pattern) in Matlab

How to select a submatrix (not in any pattern) in Matlab? For example, for a matrix of size 10 by 10, how to select the submatrix consisting of intersection of the 1st 2nd and 9th rows and the 4th and 6th columns? Thanks for any helpful answers!
v32
  • 181
  • 1
  • 1
  • 3
13
votes
1 answer

Is there a splat operator (or equivalent) in Matlab?

If I have an array (of unknown length until runtime), is there a way to call a function with each element of the array as a separate parameter? Like so: foo = @(varargin) sum(cell2mat(varargin)); bar = [3,4,5]; foo(*bar) == foo(3,4,5) Context: I…
13
votes
5 answers

Why does using "==" return a Series instead of bool in pandas?

I just can't figure out what "==" means at the second line: - It is not a test, there is no if statement... - It is not a variable declaration... I've never seen this before, the thing is data.ctage==cat is a pandas Series and not a test... for cat…
Xomuama
  • 153
  • 7
13
votes
3 answers

Python: Index an array using the colon operator in an arbitrary dimension

I have a numpy nd array. A simplified version of my task is to take a vector from along each axis. To illustrate: import numpy x = numpy.array(range(24)).reshape((2,3,4)) x0 = x[0,0,:] x1 = x[0,:,0] x2 = x[:,0,0] However I do not necessarily know…
Andrew Schwartz
  • 4,440
  • 3
  • 25
  • 58
12
votes
1 answer

Is there subscript syntax to extract a diagonal from a 2D Array?

I mostly can follow the syntax to 'drill down/slice' into an array with multiple dimensions (and flattening) on the docs page. A very cool feature. For example given: my @a=[[1,2,3], [4,5,6], [7,8,9]]; I can select column 2 of the…
drclaw
  • 2,463
  • 9
  • 23
11
votes
6 answers

How can I efficiently remove zeroes from a (non-sparse) matrix?

I have a matrix: x = [0 0 0 1 1 0 5 0 7 0]; I need to remove all of the zeroes, like so: x = [1 1 5 7]; The matrices I am using are large (1x15000) and I need to do this multiple times (5000+), so efficiency is key!
Barry S
11
votes
2 answers

How to extract elements from a matrix using a vector of indices?

Suppose I have a matrix A of order m×n and a vector of order m×1. I would like to extract elements from each row of the matrix A by using the elements of the vector as an offset in each row. For example, A = [[3, 0, 0, 8, 3], [9, 3, 2, 2, 6], …
Suyash Shetty
  • 513
  • 3
  • 8
  • 17
10
votes
2 answers

How to insert values from a vector diagonally into a matrix in R?

I need to insert a vector diagonally into a matrix on an arbitrary place. I know how to insert a vector vertically or horizontally but I can't do it diagonally. I have: A <- matrix(nrow=6,ncol=6) b <- c(1:4) The desired result (if I want to insert…
Migue
  • 1,401
  • 2
  • 15
  • 19
1
2 3
20 21