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

MATLAB: Indexing arrays with arrays of integers

MATLAB allows to index arrays with other arrays. Usually the result has the same size, but apparently this is not always the case. It must have to do with "column priority" in MATLAB. QUESTION: I am wondering how to find a consistent solution on how…
Przemek M
  • 55
  • 6
1
vote
1 answer

Select specific elements of an array in matlab

How to select the N elements then ignore the next N ones and then select the next N ones and so on? foe example of the array a = 1:100; b = 1,2,3,7,8,9,13,14,15,...
user3482383
  • 295
  • 3
  • 11
1
vote
1 answer

Indexing issue creating a matrix from parts of others

I am trying to create a master dataSheet using bits of other matrices located in the workspace. This visual representation should show what I am trying to achieve: The arrows indicate the different arrays I am trying to combine. So far 1 to 3 are…
Steve Hatcher
  • 715
  • 11
  • 27
1
vote
1 answer

Matlab concatenation possible bug

I would like to concatenate two 3d arrays in a way that supports concatenation when the left hand side is empty. I'd expect the following code to work, but Matlab (2012b, and 2013a) seems to insert an extra zero matrix at the beginning. a=[] a = …
Leo
  • 1,213
  • 2
  • 13
  • 26
1
vote
1 answer

Set a value of a specific column for each row of a matrix

I have a matrix A with m rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a (with m values). That is, I want A_{i,a_i} = 1. Is there a quick way to do this…
John Manak
  • 13,328
  • 29
  • 78
  • 119
1
vote
0 answers

Arrayed row or column indexing in Eigen

I'm trying to achieve row indexing of a matrix using an array of indices in Eigen-3.2.0 whose Matlab equivalent is the following: consider a matrix A: >> A = [2 3 0 ; 1 9 2 ; 4 7 2] A = 2 3 0 1 9 2 4 7 2 >>…
sid
  • 131
  • 3
1
vote
1 answer

Numpy subarray for arbitrary number of dimension

In Numpy, let's say you have a Nd-array A, you can slice the last dimension by doing A[...,0] or you can slice the first dimension by doing A[0]. I want to generalize this operation for all dimension (not just first or last) and i want this for…
1
vote
4 answers

Assinging a pointer to pointer of characters to a character matrix in C

The issue is this. I want to be able to create my argv using array notation: char myargv[10][30]; however main functions expect **char despite being able to treat them the same logically Ex: int main(int argc, char **argv) { …
1
vote
2 answers

Matlab general matrix indexing for accesing several rows

Edit for clarity: I have two matrices, p.valor 2x1000 and p.clase 1x1000. p.valor consists of random numbers spanning from -6 to 6. p.clase contains, in order, 200 1:s, 200 2:s and 600 3:s. What I wan´t to do is Print p.valor using a diferent…
MatlabNoob
  • 11
  • 5
1
vote
3 answers

Compute mean on matrix by groups of indices matlab

I have the following data: A = [1 2 ; 3 2; 4 7; 10 2; 6 7; 10 9] B = [1 2 3; 4 4 9; 1 8 0; 3 7 9; 3 6 8] C = [4; 10; 6; 3; 1] A = 1 2 3 2 4 7 10 2 6 7 10 9 B = 1 2 3 4 4 9 1…
matlabit
  • 838
  • 2
  • 13
  • 31
1
vote
3 answers

Group values into rows

I have a vector of information, say: Info = [10, 20, 10, 30, 500, 400, 67, 350, 20, 105, 15]; and another a vector of IDs, say: Info_IDs = [1, 2, 1, 4, 2, 3, 4, 1, 3, 1, 2]; I would like to obtain a matrix that is defined as follows: Result = …
Piwie
  • 33
  • 4
1
vote
2 answers

All pixels except one pixel in matlab

Say that we have a 3x3 matrix in matlab. If we type x(:), this will select all the elements in the matrix, right? How can we select all the elecments except element x(2,2)? What should we type in this case? Thanks.
Simplicity
  • 47,404
  • 98
  • 256
  • 385
1
vote
3 answers

Effective picking of surrounded element

If I have sequence 1 0 0 0 1 0 1 0 1 1 1 how to effectively locate zero which has from both sides 1. In this sequence it means zero on position 6 and 8. The ones in bold. 1 0 0 0 1 0 1 0 1 1 1 I can imagine algorithm that would loop through the…
user1306283
1
vote
2 answers

Complement subset in Matlab

In R, I can do the following: v <- 11:20 v[-(4:5)] and get 11 12 13 16 17 18 19 20, thus all indices except the 4th and 5th. Is there an equivalent in Matlab's indexing logic? However I wrap my mind around it, I do not seem to get the correct…
Thilo
  • 8,827
  • 2
  • 35
  • 56
1
vote
1 answer

Matlab-Subscript indices must either be real positive integers or logicals

Where did I go wrong? the problem is in: R = sqrt(bsxfun(@minus,XX,(XX)').^2+bsxfun(@minus,YY,(YY)').^2); EO = 8.8541e-12; %eps0 A2 = 1.0e-2; %2a N = 100; %num of subareas in a plate M = sqrt(N); %num of subareas in one axis DX = A2/M; % 2b DY =…
Idan Banani
  • 131
  • 2
  • 13