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

Flatten a 3D array to a 2D array using a second matrix to choose elements in third dimension

I have two input arrays: data_arr of dimensions (i,j,k) and index_arr of dimensions(i,j). The entries in index_arr are integers in the range [0, k-1]. I would like to create an output array (output_arr) of dimensions (i,j) where for each element of…
Spoonless
  • 561
  • 5
  • 14
1
vote
0 answers

Slice data vector according to a vector of indices

I have time series data stored in a long vector and I want to cut out snippets from it around each stimulus trigger. I know how to solve this with a for-loop, but I was wondering how to do it in a vectorized syntax. Consider the following minimal…
smonsays
  • 400
  • 2
  • 17
1
vote
2 answers

Combining numpys fancy indexing with slicing

I currently have a two-dimensional numpy array of shape (m, n). Furthermore, I have two (m, p) arrays of indices i1 and i2. The indices are always contiguous! import numpy as np t = np.array([[-1, -1, 0, 0, 1, 2, 2], [-1, -1, 0, …
1
vote
1 answer

Routine For Creating A Symmetric Matrix From A Given Vector In C

Lets say I have the vector x = [x0, x1, x2, x3] = [0, 2, 3, 1]. I want to create a symmetric matrix from x. Ie a symmetric matrix that has x as the first row and first column, with x0 as the diagonal. In other words, I want a matrix X like…
The Dude
  • 661
  • 2
  • 11
  • 20
1
vote
1 answer

Python: Group Unique ids and Merge Values into an Array

I have a pandas DF that looks like this DF: name ;time_cost x ;28800000, 250 x ;39600000, 300 x ;61200000, 250 x ;72000000, 0 y ;86400000, 0 y ;115200000, 250 y …
mm_nieder
  • 431
  • 1
  • 4
  • 10
1
vote
1 answer

How can I create/index this matrix in a more efficient way?

I have written a MATLAB code that works okay but I want to write it in a more efficient way (I don't want to repeat myself and I want to make it dry). At first I create a matrix of ones (28*8) and then I want to change some of its elements to minus…
ehsun
  • 137
  • 9
1
vote
1 answer

Why is advanced indexing creating a copy of a sliced matrix?

If we are assigning only a part of a matrix A to another variable (view1), this variable will only display a view of the appertaining components of the matrix. However, if the Advanced indexing is used to assign a part of a matrix A to another…
Mike
  • 191
  • 7
1
vote
2 answers

Fast indexing in C++ OpenCV

I am quite comfortable with OpenCV in Python. I recently switched to the C++ version, and I keep having problems with the cv::Mat indexing, since I'm too used to numpy's arrays. In particular, I am trying to find if there is a better way of acting…
Eggman
  • 53
  • 2
  • 14
1
vote
1 answer

Logical indexing with list comprehensions

My code currently stands as follows: z = np.diagflat(c).dot(D).dot(x); idxN, idxP = z<0, z>=0 # logical indexing y1 = [-1 + np.exp(x)/(1+np.exp(x)) for x in z[idxN]] y1 = np.array(y1) # Size (504,) y2 = [-np.exp(-x)/(1+np.exp(-x)) for x in…
srkdb
  • 775
  • 3
  • 15
  • 28
1
vote
2 answers

Taking a data.table slice with a sequence of (row,col) indices

I have a data.table that resembles the one below. tab <- data.table(a = c(NA, 42190, NA), b = c(42190, 42190, NA), c = c(40570, 42190, NA)) tab a b c 1: NA 42190 40570 2: 42190 42190 42190 3: NA NA NA Upon specification…
Joshua Daly
  • 606
  • 1
  • 7
  • 16
1
vote
1 answer

Shortest command to extract a submatrix using a vector containing the indexes of the 2 corners [matlab]

Let's say we have the following matrix A=magic(4) 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 and we want to extract 3 submatrices, identified by the indexes for top left and bottom right corners. The…
sound wave
  • 3,191
  • 3
  • 11
  • 29
1
vote
1 answer

Constructing numpy compatible memoryview interface for ndarray

I'm currently looking into (manual & high dimensional) feature extraction on very large datasets. I am encoding n2 edges in a graph in its simplest form i -> j. I'm taking advantage of that features are independent of the i -> j relationships, and…
Daniel Varab
  • 223
  • 1
  • 10
1
vote
2 answers

Manipulation of cell arrays

I have two cell arrays of size [1X5] K= {} {O1,O2,O3,O4} {O1,O3} {O1,O2,O3,O4} {O1,O2,O3,O4} W= {O3}{O2}{O2,O3}{O2,O4}{O4} I want to get as a result a cell array named S of size [1X4] as follows : I put the contents of K{i} in every S{j} where j…
StamDad
  • 135
  • 8
1
vote
1 answer

Indexing ones into a matrix of zeros without loops

I'm fairly new to MATLAB and I need some help with this problem. the problem is to write a function that creates an (n-n) square matrix of zeros with ones on the reverse diagonal I tried this code: function s=reverse_diag(n) s=zeros(n); …
1
vote
2 answers

Subsample a XY meshgrid matrix

I have a matrix of XYZ data, X and Y are in a regular "meshgrid format", I need to reduce the number of points by some factor. sample: stepXY = 1; X = 1:stepXY:100; Y = 1:stepXY:80; [Xm,Ym] = meshgrid(X,Y); XYZ = [Xm(:) Ym(:)]'; XYZ(3,:) = 7; How…
Pedro77
  • 5,176
  • 7
  • 61
  • 91