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
2 answers

applying arrayfun on n-dimensional matrixes

i need your help in solving the following problem: how can i generalize the following for any n dimensional array: reshape(arrayfun(@(x,y)sprintf('%d,%d',x,y),C{:},'un',0),size(M)); M is my matrix and C is my matrix of indexes of M. thanks in…
1
vote
1 answer

Loopless submatrix assignment in Matlab

I have a matrix F of size D-by-N and a vector A of length N of random integers in the range [1,a]. I want to create a matrix M of size D * a such that each colum M(:,i) has the vector F(:,i) starting from the index (A(i)-1)*D+1 to…
Simon
  • 5,070
  • 5
  • 33
  • 59
1
vote
1 answer

Matlab : What am I doing wrong? (Indexing)

I'm trying to solve a system of 10 linear equations out of which the middle 8 equations look alike. They look like this : t_i-1 - 2.3086*(t_i) + t_i+1 == -7.7160 where i = 2:9 so I decided to construct the coefficient matrix and the constant…
Loki93
  • 15
  • 3
1
vote
2 answers

VHDL Add specific elements from a column of a 2d array

I have a 2d array of std_logic_vector of size 10x10. I want add specific elements of a column of the array, for instance: array(4 downto 0,1) <= std_logic_vector(unsigned('0'&array(3 downto 2,2)) + unsigned('0'&array(1 downto 0,2))); But I get the…
Chris88
  • 27
  • 1
  • 4
1
vote
2 answers

numpy, return of array of indices in shape of

I want to get the result of a list (or array) of indices from a numpy array, in the shape: ( len(indices), (shape of one indexing operation) ). Is there any way to use a list of indices directly, without using a for loop, like I used in the mininal…
sklingel
  • 169
  • 2
  • 9
1
vote
1 answer

how to select rows of a matrix on the basis of occurrence of elements in matlab?

i have a [sentence cross words] matrix as follows. x = [ 0 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 1 1 0 0 0 0 0 ]; 0 shows a word is present in respective sentence and 1 shows a word is absent in the respective sentence, i have…
Afzaal Ahmad
  • 187
  • 1
  • 12
1
vote
1 answer

Autocorrelation with linear indexing of 2D matrix

I have a matrix xM which is indexed linear and when I try to calculate the autocorrelation for every column of it, I get an error that the max lag must be integer. Maybe I shouldn't use the ind2sub function? Please help, thanks in advance. xM = x(…
user_dsp
  • 67
  • 1
  • 6
1
vote
1 answer

Find local maxima with logical indexing in MATLAB

I have used logical indexing before in MATLAB vectors for conditions like X = X(X < 6); Now however I would like to find local extrema using the same idea, but with "local" conditions. I would be interested in something like X = X(X(i) > X(i-1) &…
nabla
  • 235
  • 2
  • 11
1
vote
2 answers

Extracting matrix rows with variable step

Let's say that i've a 3 columns matrix like this one: 1 2 0,1 "A" 1 3 0,2 "B" 1 4 0,3 "C" 1 5 0,4 1 6 0,5 1 7 0,6 1 8 0,7 1 9 0,8 1 10 0,9 1 11 1 2 3 1,1 "A" 2 4 1,2 "B" 2 5 1,3 "C" 2 6 1,4 2…
gmeroni
  • 571
  • 4
  • 16
1
vote
1 answer

Matlab: extract submatrix with selecting some values from the last line

20 4 4 74 20 20 74 85 85 85 A = 36 1 1 11 36 36 11 66 66 66 77 1 1 15 77 77 15 11 11 11 3 4 2 6 7 8 10 10 15 17 how from the matrix A, I can…
bzak
  • 563
  • 1
  • 8
  • 21
1
vote
1 answer

Sorting Elements in Groups - MATLAB

I have an array A having x and y values.. e,g. A = 5 1 7 2 3 1 5 3 4 4 7 3 2 5 9 5 7 6 I need to take three values at one time and sort them in ascending order according to the x values (First 3 x y…
Agror
  • 37
  • 9
1
vote
2 answers

Indexing in numpy ndarray by dynamcaly generated indexes

I have an image imported in a ndarray with a shape (1027, 888, 3). My task is to create a method that returns 2 one dimensional arrays of indexes that select a tile from the image. ii, jj = tile_cordinates(i,j, tile_size) imshow(image[ii,jj]) I…
1
vote
2 answers

C++ indexing syntax: two libraries use different indexing syntax: 0 based and 1 based index

As shown in the following figure, I have two libraries in my C++ code which use different indexing syntax, one starting with 0 another starting with 1. The C++ code is pretty sophisticated and this difference is very troublesome for me. I was…
Megidd
  • 7,089
  • 6
  • 65
  • 142
1
vote
1 answer

How to find values based on row and column data

I have a matrix with the first row and the first column are data. Based on the row and the column, I wanna to find values in the matrix. For example, I have the matrix look like this A = 0.1000 1.0000 10.0000 100.0000 1.0000 0.9000 …
Hung
  • 13
  • 2
1
vote
1 answer

What is wrong with this line of MATLAB code?

When I try to run this line of code in MATLAB: image_arr(i,:,:) = medfilt2(image_arr(i,:,:), [9 9]) it errors: Error using medfilt2 Expected input number 1, A, to be two-dimensional. image_arr is an n * x_max * y_max array containing n, x_max by…
NGXII
  • 407
  • 2
  • 9
  • 18