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

removing rows from matrix based on cell array value

I have 2 cell arrays and one matrix. The first cell array called 'all_ids' is a 6650 x 1 cell which contains strings. The second cell array called 'needed_ids' is a subset of 'all_ids' and is 6600 x 1 cell which also contain strings. The matrix…
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
2
votes
2 answers

Logical Arrays - In an assignment A(I) = B, the number of elements in B and I must be the same

I have three matrices, A, B and C. When B is larger than A, I want to saturate the value with A. It says that the number of elements in I (which is (B > A)) must be the same as the number of elements in A. I checked below and they are the same. >>…
2
votes
2 answers

Matlab: column of empty matrix

Wondering if anyone could help me with this. For getting the first column x of a matrix A. I use x = A(:,1). Every so often, the matrix A is empty, in which case I would like my column to also be empty. But with Matlab, the code exits with error…
2
votes
2 answers

Submatrix in C++

I have a programming issue regarding the extraction of a subimage (submatrix) from a bigger image (matrix). I have two points (upper and lower bound of the subimage i want to extract) and i want to extract the subimage from the bigger one based on…
mad
  • 2,677
  • 8
  • 35
  • 78
2
votes
3 answers

Access predefined elements of cells

I have a cell array A [1x80] in which each element is a cell array itself [9x2]. I have also a vector B representing a group of selected cells of A and I want to extract the element {2,2} of each selected cell. I tried with a…
gabboshow
  • 5,359
  • 12
  • 48
  • 98
2
votes
1 answer

Multiple left-hand-side partial assignment in Matlab

Consider the following matrix: a=[1,2,3] therefore size(a)=[1,3] I want to assign the second dimension 3 to variable n. What is the most efficient way? Why are the following not working? [[],n]=size(a) or n= num2cell(size(a)){2}
godblessfq
  • 218
  • 1
  • 10
2
votes
3 answers

Coverting C style code into Matlab

I've the following code in C: for(i=0;i
Malice
  • 1,457
  • 16
  • 32
2
votes
1 answer

How to apply logical indexing on multi-dimension matrix in matlab

My question is simple. I have an rgb image and a logical matrix. I want to set the pixel which is true in the corresponding element of logical matrix to (150,160,170). For example: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 …
oibook13
  • 21
  • 3
2
votes
2 answers

numpy: find all occurences of elements from one table in another

I have a problem of translating elements in a numpy array according to a translation table given. Say I have a 2D translation table trTab, e.g. import numpy as np trTab = np.array([[0, 1, 2, 3 ,4, 5, [5, 2, 3, 1, 0, 4]]) where…
2
votes
2 answers

Retrieving the elements of a matrix with negated exact indexing with index matrix?

For example I have A=[11 24 33 47 52 67] and I have indices matrix as I = [2 3] so I want to have the elements of A from the indices other than indices given with I. So I want to have B = [11 47 52 67]. How can I do it and use I as a negated indices…
erogol
  • 13,156
  • 33
  • 101
  • 155
1
vote
2 answers

How to extract non-vertical column from matrix in Matlab

I have matrix A and a vector b, which specifies column index of the element to be extracted for each corresponding row of the matrix. For example, A = [1 2 3 4 5 6 7 8 9] b = [1 3 2]' I'd like to have c = [1 6 8]' on output. How to…
user979454
  • 11
  • 1
1
vote
1 answer

Matrix index out of range during loop

a is an nxn matrix. I have this code: [m,n] = size(a); x = zeros(m,1); for j=1:1:n if(j==1) a(1,:) = []; else end disp(a); a(:,j) = []; disp(x); disp(a); end And it gives error on the line a(:,j) =…
Jun Seo-He
  • 131
  • 5
1
vote
0 answers

How to modify a masked array in place directly by assigning to it using advanced indexing

I have an array that I want to change some values on some rows. The desired rows will be addressed by a Boolean masked array. Then I want to modify one of the values in the rows: a = np.array([[0., 0.], [0., 0.], [0.,…
Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
1
vote
1 answer

How to sort a one hot tensor according to a tensor of indices

Given the below tensor: tensor = torch.Tensor([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 0., 1.], [1., 0., 0., 0., 0.], …
lima0
  • 111
  • 6
1
vote
1 answer

Flat indexing of all but first dimension with Numpy

Is there some way to use flat indexing for the remaining dimensions with NumPy? I'm trying to translate the following MATLAB function to Python function [indices, weights] = locate(values, gridpoints) indices = ones(size(values)); weights =…
Fredrik P
  • 682
  • 1
  • 8
  • 21