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

R programming: How to do replace values by row?

How to do a row-wise replacement of values using R? I have a Matrix and I would like to replace some of its values using an index vector. The problem is that R automatically does a column-wise extraction of the values as opposed to a row-wise. You…
Kinin
  • 55
  • 6
0
votes
1 answer

Select all BUT certain index pairs in multi-dimensional array Matlab

I am trying to select all BUT certain index pairs in a multi-dimensional array. i.e. I have a set of paired indices (e.g. [1,2] and [4,5]). I want to set all BUT those indexed pairs to 0. The closest I have come to this…
0
votes
0 answers

Assignment in large sparse matrices Matlab

I have one very large tall sparse matrix1 (MxN , where M >>>> N) I want to assign chunks of it to two other very large sparse matrices of size matrix2: MxN1 and matrix3: MxN2 (N1 + N2 != N) What I am doing right now is matrix2(1:rowIndex1,…
Rahul
  • 3,220
  • 4
  • 22
  • 28
0
votes
0 answers

Matlab indexing of function

Let's say I have a matrix A and want to calculate the standard deviation by using, std(A,0,2). If I don't want to use the first row in further calculations, I can write somthing like B = std(A,0,2) and use B(2:end,:) but is there a way to make this…
Oene
  • 1
  • 1
0
votes
0 answers

Index exceeds matrix dimensions in image

clc; rgb = imread('apple.jpg'); rgb = imresize(rgb,[200,200]); mlab = makecform('srgb2lab'); lab = applycform(rgb,mlab); k=1; for i=1:k*3:200 for j=1:k*3:200 m = region(i,j,lab); [a,b,c] = size(m); m1 = region(1,1,m); …
hardik
  • 378
  • 5
  • 18
0
votes
0 answers

Index n-th dimension of Numpy array with 2d index array

I have the following 2-D Numpy arrays: X # X.shape = (11688, 144) Y # Y.shape = (2912, 1000) The first array is populated with atmospheric data, and the second array is populated with random index values from 0 to X.shape[0]-1. I want to index the…
Lcg3
  • 93
  • 1
  • 5
0
votes
1 answer

Extracting a submatrix that contains some sub-columns from A

20 4 4 74 20 20 74 85 85 85 2 1 A = 36 1 1 11 36 36 11 66 66 66 4 1 77 1 1 15 77 77 15 11 11 11 1 4 3 4 2 6 7 8 10 10 …
bzak
  • 563
  • 1
  • 8
  • 21
0
votes
0 answers

Matching Numpy and NetCDF4 indexing when creating a netCDF file

I'm trying to move values from a numpy array to a NetCDF file, which I am creating. Currently I'm trying to find the best way to emulate 'fancy indexing' of numpy arrays when creating a netCDF file, but the two indexing systems don't match when the…
0
votes
1 answer

Reordering Cell Array by Array of Indices

Suppose I have a cell array x and an integer array y: x = {'apple', 'orange', 'banana', 'pear'}; y = [2 4 3 1]; In fact, y represents indices of x. I want to now create a cell array z with the elements of x reordered as specified by the order…
Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
0
votes
3 answers

Numbering of array indices in Julia

I seem to find that I get an bounds error when I try to reference array[0]. So it seems that julia has elected to necessarily use the same convention that MATLAB uses, starting the indices from 1. Is this true? Any reason for that choice?
comer
  • 311
  • 1
  • 3
  • 10
0
votes
1 answer

indexing and substitution of xts elements

I have these two object: sig (class matrix) and xts (class xts) In the first object I want to find the position where this condition is satisfied: "sig != mlag(sig) & sig != 0" When I have these positions I want to substitute value in the xts object…
Fryc
  • 93
  • 1
  • 1
  • 7
0
votes
2 answers

How to assign different index numbers to determined observations?

What I want to do is assign a value of 1 to the first 1/3 of observations ofmy data, then a value of 2 to the second 1/3 of observations of my data and finally a value of 3 to the third 1/3 of observations of my data. Taking into a ccount that my…
csmontt
  • 614
  • 8
  • 15
0
votes
0 answers

Vectorize the populating of a high-D array

I'm populating distribution testa with observations test_idxs test_idxs <- matrix(sample(c(1,2,3), 300000, repl=T), ncol=3) testa_for_looped <- array(0, c(3,3,3)) testa_vectorized <- array(0, c(3,3,3)) system.time( for (i in 1:nrow(test_idxs)) {…
enfascination
  • 1,006
  • 9
  • 20
0
votes
1 answer

Fast matrix indexing from vectors

I want to do a lot of matrix indexing of a high-D array, but the indices are split up. I came up with a few solutions: ### setup test <- array(0, c(3,3,3,3)) test[1,2,3,2] <- 1 system.time(for (i in 1:1000000) test[1,2,3,2] ) ### index split…
enfascination
  • 1,006
  • 9
  • 20
0
votes
2 answers

Matlab : Matrix indexing Logic

i am doing very simple Matrix indexing examples . where code is as give below >> A=[ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ] A = 1 2 3 4 5 6 7 8 9 10 11 12 >> A(end, end-2) ans = 10 >>…
user2088260