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
1 answer

Index array of every element in MATLAB?

This might be a basic question. To find out the maximum value and its index in array in MATLAB, I have used this code: A = [1 2 3; 4 5 6; 7 8 9] [val, idx] = max(A, [], 2); Now, how can I find the index array of all the element (not finding…
ShadowWarrior
  • 180
  • 1
  • 12
0
votes
2 answers

Matrix Indexing Question in Matlab

Possible Duplicate: Can someone explain this example of deleting elements from a matrix in MATLAB? i have some trouble in matlab and please help me suppose we have this matrices X = 16 2 13 5 11 8 9 7 12 4 14 1 i want to understand how…
user466534
0
votes
1 answer

Vectorizing array access from indices matrix

Consider the following: In [51]: arr = np.arange(6, 10) In [52]: idx = np.random.randint(4, size=(3, 4)) In [53]: idx Out[53]: array([[0, 3, 3, 1], [1, 3, 3, 2], [1, 1, 1, 1]]) In [54]: result = np.empty_like(idx) In [55]: for i in…
galah92
  • 3,621
  • 2
  • 29
  • 55
0
votes
1 answer

loop over a sequence and rounding problem in R

I want to assign some value to a vecter like: a = rep(0, 101) for(i in seq(0, 1, 0.01)){ u <- 100 * i + 1 a[u] <- u } a plot(a) The output is > a [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 …
Carlton Chen
  • 615
  • 5
  • 9
0
votes
1 answer

Read matrices as cell array from long longitudinal matlab data

Assume the following hypothetical Matlab data (as column vectors): for 3 subjects (i=1 to 3) each provides three measurements y1, y2 , y3, over 5 time points (j=1 to 5) or less (unbalanced). The original data set is bigger. so I need to use cell…
N. I. ElZayat
  • 11
  • 1
  • 3
0
votes
2 answers

Finding coordinates min and max of a matrix without using min/max commands

I have this code that shows me min and max values from a random matrix without using min/max commands: m = rand(5,5)*10 mn = m(1); mx = m(1); for ii = 2:numel(m) if m(ii) < mn mn = m(ii); imn = ii; …
Lucas Tesch
  • 147
  • 1
  • 3
  • 11
0
votes
2 answers

Indexing a set of indices to a matrix

I retrieved all non-white pixel from an image : [ii, jj] = find(BlackOnWhite < 255) Then I tried to index those pixel coordinates to a matrix : image(ii, jj) = 0 But zeros do not appear in the expected places. How can I put zeros only in the…
Rom
  • 225
  • 2
  • 11
0
votes
1 answer

Logical indexing using a mask works in numpy, not in Matlab

I'm trying to reproduce the following Python code in MATLAB, using a sparse matrix. >>> print(M) [[0 0 0 0 0] [0 1 1 1 0] [0 1 0 1 0] [0 1 1 1 0] [0 0 0 0 0]] >>> im2var = np.arange(5 * 5).reshape((5, 5)) >>> A = np.zeros((25, 25),…
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
0
votes
1 answer

Assign zero to multiple columns in each row in a 2D numpy array efficiently

I want to assign zero to a 2d numpy matrix where for each row we have a cutoff column after which should be set to zero. For example here we have matrix A of size 4x5 with cutoff columns [1,3,2,4]. What I want to do: import numpy as…
Ash
  • 3,428
  • 1
  • 34
  • 44
0
votes
3 answers

Selectively access rows of numpy 3D matrix

Is there a way to index a 3D numpy matrix to selectively grab the i'th row of each i'th layer? e.g. I have an RxNxR matrix, and I want to grab the 1st row of the 1st layer, the 2nd row of the 2nd layer, the 3rd row of the 3rd layer, etc. and end up…
0
votes
2 answers

Reshaping a 3d matlab matrix into 2d matrix with indices and values

I have a 3D matrix in MATLAB. It has 3 rows, 4 columns and 2 time frames. Please see the dataset: >> size(filtered_data) ans = 3 4 2 >> filtered_data filtered_data(:,:,1) = 15 22 19 16 15 15 13 17 19 20 …
user2273202
0
votes
3 answers

How to skip the index when the indexing goes out of range [Python]

My matrix is : [['f', 'e', 'e', 'd'], ['t', 'h', 'e', 'd'], ['o', 'g']] Code : for i in range(cols): result = "" for j in range(rows): result += matrix[j][i] temp.append(result) return("…
Aditya Kushwah
  • 343
  • 1
  • 2
  • 8
0
votes
1 answer

Matrix Creation and Assignment in Matlab

I have two matrices A fixed size, B grows iterationwise. How to create matrix B of equal size as A, but to be enlarged/manipulated according to the vector 'j'. Cases 1) Size B< Size A, 2) Size B=Size A, 3) Size B> Size A. First case is…
0
votes
2 answers

How to sort table according to multiple columns at a time?

I have table with 16 columns, first three are input columns. I want to sort whole table according to first three columns at a time. T = table(a1, a2, a3, ..., a16) All a1, a2, and a3 will be sorted in ascending order. For example: a1 = [6 3 9 6 3 9…
user9003011
  • 306
  • 1
  • 10
0
votes
1 answer

Indices such that elements are in the closed interval

I got a numpy 1d arrays, and I want to find the indices of the array such that its values are in the closed interval specified by another 1d array. To be concrete, here is an example A= np.array([ 0.69452994, 3.4132039 , 6.46148658, …
Shew
  • 1,557
  • 1
  • 21
  • 36