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
2
votes
1 answer

How to index sparse tensors in TensorFlow?

Is there a way to index a cell/section of a sparse tensor like in case of a dense tensor? # dens_tensor.shape = [10, 10, 10] dense_cell = dense_tensor[0,0,1] If not, does someone know a workaround to index a sparse tensor?
spreisel
  • 391
  • 1
  • 3
  • 7
2
votes
1 answer

How to explicitly get linear indices from arrayfire?

Suppose I have an stl::array foo which is the linearized STL pendant to a Column-Major format arrayfire array, e.g. af::array bar = af::array(4,3,2, 1, f32);. So I have an af::dim4 object dims with the dimensions of bar, I have up to 4…
E. Odj
  • 81
  • 5
2
votes
1 answer

How to access n-D matrix with n index vectors?

I have a matrix A = repmat(1:7,7,1); I have index vectors idx1 = [1 3 5]; idx2 = [1 3 5]; I want to access A at the 2d coordinates denoted by idx1(i),idx2(i). When I do A(idx1,idx2) = 0; I get for each element in idx 1, all the elements in idx2…
Gulzar
  • 23,452
  • 27
  • 113
  • 201
2
votes
1 answer

modifying sparse matrix using advanced indexing in python

I'm trying to use advanced indexing to modify a big sparse matrix. Say you have the following code: import numpy as np import scipy.sparse as sp A = sp.lil_matrix((10, 10)) a = np.array([[1,2],[3,4]]) idx = [1,4] A[idx, idx] += a Why this code…
aaragon
  • 2,314
  • 4
  • 26
  • 60
2
votes
2 answers

How can I find column in matrix that matches 2 rows in matlab?

I have a matrix, that I want to find a column that has item in row1 == x, and item in row2 == y; What is the fastest way to do this? Thanks, CP
CptanPanic
  • 609
  • 8
  • 22
2
votes
2 answers

How to lookup an item by multiple indexes in matlab

I have a n+1 column matrix. I want a function mySearch(idx1,idx2...,idxn) that returns the n+1'th column in the row whose first n elements are equal to idx1...idxn Next step, i want mySearch to return a value which is closest to the indexes, by some…
Gulzar
  • 23,452
  • 27
  • 113
  • 201
2
votes
2 answers

Numpy advanced selection not working

Can someone please help me to understand why sometimes the advanced selection doesn't work and what I can do to get it to work (2nd case)? >>> import numpy as np >>> b = np.random.rand(5, 14, 3, 2) # advanced selection works as expected >>>…
orange
  • 7,755
  • 14
  • 75
  • 139
2
votes
1 answer

Grow 3D array in Matlab

Is there a way to grow a 3D array in the third dimension using the end index in a loop in Matlab? In 2D it can be done like a = []; for x = y a(end + 1, :) = f(x); end But in 3D the same thing will not work as a(1,1,end) will try to index…
zephyr
  • 159
  • 1
  • 11
2
votes
4 answers

Matlab find does not work with high dimensional array?

say A = rand(2,2,2); [a,b,c] = find(A == A(1,2,2)) I got a=1 b=4 c=1 what?
rick
  • 23
  • 3
2
votes
3 answers

Matlab: Copy a diamond portion of an array

Suppose the array is: A = a b c d e f g h i I want to copy the diamond arrangement, i.e. the elements d,b,f,h,e into a new 1D array. The above array is just an example, the matrix could be any size rectangular matrix and the…
HIMANK
  • 35
  • 3
2
votes
1 answer

Matrix Indexing with larger matrix

I'm trying to understand some old code from a predecessor and I'm having some problems with a certain kind of matrix indexing: I have a large matrix A that has labelled regions (neighboring elements that share a number) Now I have a second matrix…
DarkCell
  • 170
  • 1
  • 11
2
votes
3 answers

Why does (:) with no values behave differently?

Given the following example: >> I=[2 1 3;3 2 4] I = 2 1 3 3 2 4 >> I(:) ans = 2 3 1 2 3 4 >> I(1:2) ans = 2 3 Why does I(:) return a column vector while I(1:2) returns a…
user198729
  • 61,774
  • 108
  • 250
  • 348
2
votes
2 answers

How can I use values within a MATLAB matrix as indices to determine the location of data in a new matrix?

I have a matrix that looks like the following. I want to take the column 3 values and put them in another matrix, according to the following rule. The value in the Column 5 is the row index for the new matrix, and Column 6 is the column index.…
2
votes
2 answers

How to use logical values and select data in another matrix?

I am getting frustrated with MATLAB when I tried to get the data back in matrix format. But every time i only get back the answer in a single column format. I will illustrate my question: For example, A = [1 -3 2;5 4 7;-8 1 3]; L =…
2
votes
2 answers

Reshaping a matrix in Matlab by selecting rows at a fixed distance

I have a matrix D in Matlab of dimension (a*b)xc with the following structure: suppose a=3, b=4, c=3 D=[1 1 10; 1 2 11; 1 3 17; 1 4 15; 2 1 68; 2 2 6; 2 3 15; 2 4 7; 3 1 5; 3 2 43; 3 3 0; 3 4 5]; The…
TEX
  • 2,249
  • 20
  • 43