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 remove rows from pandas dataframe that contain combinations of the first two columns

I am trying to get rid of the rows that contain element combinations of the first two columns in a pandas dataframe, for instance, in the next df: event1 event2 uccs ulags 0 327423.0 329243.0 0.1663 -0.6013 1 327423.0 329589.0 …
2
votes
1 answer

Selecting faces of a mesh based on vertices coordinates in numpy

I have two numpy arrays, one is for 3D vertices of a mesh, call it vert and one is for the triangular faces, call it faces: The vert array is a N x 3 shape array of float, hence N three dimensional points. The x coordinate of each point can have…
linello
  • 8,451
  • 18
  • 63
  • 109
2
votes
2 answers

NumPy: Unsort/undo a reverse/descending sort

I can do an in-place reverse sort (descending sort) of a numpy array, but I also need to be able to unsort (undo) it later. Given an unsorted example: a = np.array([-1, -2, 1, -3, 2, 0]) I tried: i = a[::-1].argsort().argsort() # BAD attempt to…
Asclepius
  • 57,944
  • 17
  • 167
  • 143
2
votes
1 answer

Generate all possible combinations of pairs of triplets from 3 vectors together with their original coordinates

Given 3 row vectors A,B,C in Matlab, I want to generate the matrix D reporting all the possible combinations of pairs of triplets from A,B,C together with their original coordinates in A,B,C. I have written a code that does what I want. As I'm…
TEX
  • 2,249
  • 20
  • 43
2
votes
1 answer

How can I add to each element of a numpy ndarray the sum of all its index values with python ?

I have a numpy ndarray, let's take an example (but it can be totally different): [[[0 0 0] [1 1 1] [0 0 0]] [[1 0 1] [1 0 1] [1 0 1]]] [[1 0 0] [1 1 0] [1 1 1]]] I want to add to each element the sum of its indexes in all…
Cépagrave
  • 189
  • 1
  • 2
  • 12
2
votes
1 answer

Matrix without a particular submatrix in R

I have randomly generated a submatrix “train” from “mat” using the following: train <- mat[sample(nrow(mat), size = 317, replace = FALSE),] My question is, how do I then create “test” as a submatrix of “mat” which excludes the matrix “train”?…
TuringTester69
  • 177
  • 1
  • 9
2
votes
4 answers

Extracting every n-th column from matrix

I need to extract every n-th column from my matrix. For example I have a matrix: A = 1 23 34 53 67 45 67 45 12 34 45 56 67 87 98 12 1 2 3 45 56 76 87 56 And I want to…
Aybars
  • 395
  • 2
  • 11
2
votes
1 answer

Combining a LGR and a Kalman Filter into a single control

What I'm trying to do I'm trying to create a LQG controller to control a given system by combining a Linear Quadratic Regulator (LQR) and a Kalman Filter. Where I'm stuck I have found both separately, but am unsure how I combine them in MATLAB.…
2
votes
2 answers

MATLAB store indices maximum in logical matrix

Lets say I have a 4 dimensional matrix, from which I would like to retrieve the maximum values over the 2nd and 3rd dimension. A = rand(4, 4, 4, 4); [max_2, in_2] = max(A, [], 2); [max_3, in_3] = max(max_2, [], 3); How could I use ind_2 and ind_3…
hasdrubal
  • 1,024
  • 14
  • 30
2
votes
1 answer

Index A Matrix Using A Vector of Indices in Armadillo LIbrary

I'm using the Armadillo Cpp code for matrix algebra. I have an Eigenvector matrix E that I want to sort by its eigenvalues in a vector d. mat E; vec d; eig_sym(d,E,Rxx); // Sort indices of eignen values / vectors // based on decreasing real part…
farhanhubble
  • 476
  • 3
  • 18
2
votes
2 answers

Error using sub2ind (line 52) Out of range subscript Matlab

I used the same code for two different input matrices, In both cases i will call it "the input matrix A" The first case is a 7000X4 The second case is a 29500X12 I need to split a selected column in windows and then for each window i need to…
Andrea Ciufo
  • 359
  • 1
  • 3
  • 19
2
votes
2 answers

matlab: find and replace elements of matrices within a cell array

I have a cell array that contains multiple matrices of different size. I'd like to find & replace all elements of the matrices by a condition, e.g. replace all 1's with 0's. I found a temporary solution from find and replace values in cell array,…
Leander Moesinger
  • 2,449
  • 15
  • 28
2
votes
2 answers

Is there a built-in (no for loop) way to put ranges of a vector into a cell array using a matrix for the indices?

Given: data = 0:10; indices = [1 2; 3 7; 2 5]; Is there a one-line way to do this?: for i = 1:length(indices) out{i} = data(indices(i,1):indices(i,2)) end
2
votes
3 answers

How do I represent and access elements of this multivalued data structure?

I am trying to implement the Brooks-Iyengar algorithm for sensor fusion, and am trying to represent the following data structure in MATLAB. A = {([1.5,3.2],4), ([0,2.7],5), ([0.8,2.8],4)} I tried doing the following B =…
2
votes
1 answer

3d matrix: how to use (row, column) pairs with 3rd dimension wildcard in MATLAB?

I have a 3 dimensional matrix, and a list of (row, column) pairs. I would like to extract the 2 dimensional matrix that corresponds to the elements in those positions, projected through the depth of the matrix. For instance, suppose, >> a = rand(4,…