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

What is the tensorflow equivalent of numpy tuple/array indexing?

Question What is the Tensorflow equivalent of Numpy tuple/array indexing to select non-continuous indices? With numpy, multiple rows / columns can be selected with tuple or array. a = np.arange(12).reshape(3,4) print(a) print(a[ (0,2), #…
mon
  • 18,789
  • 22
  • 112
  • 205
1
vote
1 answer

How to interpret numpy advanced indexing solution

I have a piece of numpy code that I know works. I know this because I have tested it in my generic case successfully. However, I arrived at the solution after two hours of back and forth referencing the docs and trial and error. I can't grasp how I…
chr0nikler
  • 468
  • 4
  • 13
1
vote
1 answer

Indexing [::-1] to Reverse ALL 2D Array Rows and ALL 3D and 4D Array Columns and Rows Simultaneously Python

How do you get indexing [::-1] to reverse ALL 2D array rows and ALL 3D and 4D array columns and rows simultaneously? I can only get indexing [::-1] to reverse 2D array columns. Python import numpy as np randomArray =…
Jeremy
  • 113
  • 2
  • 14
1
vote
1 answer

numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?

Question Why are the numpy tuple indexing behaviors inconsistent? Please explain the rational or design decision behind these behaviors. In my understanding, Z[(0,2)] and Z[(0, 2), (0)] are both tuple indexing and expected the consistent behavior…
mon
  • 18,789
  • 22
  • 112
  • 205
1
vote
0 answers

Pytorch tensor indexing clarification

Consider the following: a = torch.rand(2, 3, 4) tensor([[[0.2410, 0.3700, 0.9221, 0.5289], [0.8820, 0.2856, 0.4072, 0.6177], [0.4279, 0.9396, 0.7483, 0.0087]], [[0.7295, 0.4965, 0.9559, 0.0419], [0.7379, 0.5761,…
Gilad
  • 538
  • 5
  • 16
1
vote
0 answers

Change numpy array values by giving corner indices

i am trying to change certain matrix values of a 2D numpy array. I need this to classify areas of an image in pixel level. Imagine having a matrix like: a = [ [1, 2, 3, 4 ] [5, 6, 7, 8 ] [9, 10, 11, 12] [13, 14, 15, 16]…
AEM
  • 13
  • 5
1
vote
1 answer

Fancy indexing in tensorflow

I have implemented a 3D CNN with a custom loss function (Ax' - y)^2 where x' is a flattened and cropped vector of the 3D output from the CNN, y is the ground truth and A is a linear operator that takes an x and outputs a y. So I need a way to…
sid
  • 13
  • 2
1
vote
1 answer

How to concatenate 3 RGB channels without using the cat(3,R,G,B) function

I have a RGB image which I am trying to complement without using the built-in function imcomplement(A). R=A(:,:,1);% A is the input image G=A(:,:,2); B=A(:,:,3); [r,c]=size(B); for i=1:1:r for j=1:1:c R(i,j)=255-R(i,j); …
1
vote
1 answer

Pytorch: accessing a subtensor using lists of indices

I have a pair of tensors S and T of dimensions (s1,...,sm) and (t1,...,tn) with si < ti. I want to specify a list of indices in each dimensions of T to "embed" S in T. If I1 is a list of s1 indices in (0,1,...,t1) and likewise for I2 up to In, I…
1
vote
1 answer

Rewriting parts of a string inside an index

having a little bit of trouble indexing parts of a list in Python 3. The prompt/question is below and I have already made the input text file. Prompt: You are given a file called class_scores.txt, where each line of the file contains a…
Lachlan M
  • 13
  • 3
1
vote
1 answer

Plot multiple 2D graphs stored as a 3D matrix

I need to combine a fair amount (~15000) of plots on a single figure. Doing it iteratively would take forever, so I stored all my data in a 3D matrix hoping I could plot all my data at once. My matrix structure is as following : So, if I want to…
1
vote
0 answers

How can I use a 2D matrix as an index for 4D matrix?

How can I use a 2D matrix as an index for 4D matrix? I have: A stack of seven images (15x10x3x7) in a 4D matrix called images in the form [Y, X, RGB, imageIndex] A corresponding index matrix indexes (15x10) which stores which image each pixel come…
DarkMatterMatt
  • 576
  • 10
  • 22
1
vote
2 answers

Split array based on given indices

I want to split one array into two based on given indices. Specifically, have two arrays, one array A with data (3 columns) and one B with indices. A = [10 11 12; 20 21 22; 30 31 32; 40 41 42] B = [1 3] As a result, I want two…
Mella 30
  • 13
  • 4
1
vote
2 answers

Can someone explain this example of deleting elements from a matrix in MATLAB?

The following example appears in the MATLAB tutorial: X = [16 2 13; 5 11 8; 9 7 12; 4 14 1] Using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector.…
Dónal
  • 185,044
  • 174
  • 569
  • 824
1
vote
1 answer

Fancy Indexing with Boolean Masking | Numpy in Python

I came across this piece of code in the Python Data Science Handbook, have modified it here for readability. It is quite puzzling for me, given it combines fancy indexing with masking and I am unable to understand what is happening…
pragun
  • 471
  • 1
  • 6
  • 9