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

Matrix Manipulation based on threshold vectors in R

I have a numeric matrix as follows 1 2 3 4 5 1 4 6 1 4 2 4 1 6 8 1 7 3 6 7 1 4 5 6 2 I have a vector c(2,4,2,6,8) For each row I want to take the corresponding value in the vector and make a new matrix where all the values in the row greater than…
0
votes
3 answers

Matlab: How can I shift columns independently in a matrix?

I have a matrix of 10X3 and I want to make a new matrix using only a subset of each column but I want the subset to be different for each column based on an index array and a defined range from that point. For example if the matrix is (the numbers…
Ben
  • 11
  • 3
0
votes
2 answers

Eliminating part of arrays in matlab

Assume that we have a 100*4 array. We also have a 100*1 array of 1 and 0. Assume there are n 1's. We want to create a n*4 array from the 100*4 array, where we only include the columns for which the second array is a 1. One way to do it is through a…
0
votes
2 answers

Matlab : how to get M(1,2) from vector v=[1,2]?

Say I have: M = [1, 2; 3, 4] v = [1, 2]; M(v) gives [1 3] but I want to get is M(1, 2) = 3. Is there a way to do it? Of course, I could do M(v(1), v(2)) but I need the thing to work for M an N-dimensional array and v a vector of length N. Thanks…
JuliaR
  • 99
  • 5
0
votes
2 answers

Create a new matrix from indices

I have huge n×n matrix A, and the indices of its non-zero elements by a = find(A). I have obtained a new list a1 by deleting some elements from a. I want to have matrix A of indices in a1 without using loops. Any suggestions? Is there any function…
Fatime
  • 197
  • 1
  • 5
  • 15
0
votes
2 answers

Finding index of vector from its original matrix

I have a matrix of 2d lets assume the values of the matrix a = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 17 24 1 8 15 11 18 25 2 …
Aiman
  • 11
  • 1
  • 4
0
votes
1 answer

array as index of a vector in matlab

I stumbled upon some matlab code where a 1D vector (say "signal" with length 100) is indexed by a 3D matrix (say "distances" with dimensions 10x10x10) and the result ("signal(distances)") is a 3D matrix with exact same dimensions 10x10x10 as the 3D…
BQ92-PwDD3
  • 13
  • 3
-1
votes
1 answer

How to convert topk data from torch.tensor.topk() to a pytorch tensor

I have a 1-d pytorch tensor and I got topk data of the tensor and indeces of this datas. How can I place each data in the corresponding position of a empty pytorch tensor? The topk of this tensor elements and their indeces is got by: value, indeces…
CXLi
  • 3
  • 1
-1
votes
2 answers

How to swap values in a 3-d array in Python?

I have a matrix (3x5) where a number is randomly selected in this matrix. I want to swap the selected number with the one down-right. I'm able to locate the index of the randomly selected number but not sure how to replace it with the one that is…
jmar225
  • 3
  • 1
-1
votes
1 answer

Filtering 3D matrix above certain threshold?

I have two 3D matrices of the same size. Say, A contain values ranging from 0 to 1 and B contains certain discrete values. I want to extract values from B matrix which are above certain threshold values in A? Can anyone help me out?
-1
votes
1 answer

Logical indexing: What is going on here?

I have read this documentation on logical indexing, but it doesn't clarify my problem. I have this line of code: y = rand(20,3); aa= unidrnd(2,20,3) - 1; val = ( aa & y<1.366e-04) | (~aa & y<8.298e-04); aa(val) = ~aa(val); I cannot understand what…
nashynash
  • 375
  • 2
  • 19
-1
votes
1 answer

Converting a list of indices of a cell array

I have an array x: x = [2, 1, 2, 3, 3, 2] I also have a cell array y: y = {'alpha', 'beta', 'gamma'} In fact, x is a list of indices, where each indicates an element in y. I now want to create another cell array z, where each index in x is…
Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
-2
votes
1 answer

Matlab/ GNU octave syntax

I need to understand a part of the GMRES algorithm written in these languages. What makes me a problem is the following piece: y = H(1:k,1:k) \ beta(1:k); x = x + Q(:,1:k)*y; Can someone explain what does it mean in extended form. Thank you in…
yarchik
  • 336
  • 1
  • 8
-2
votes
1 answer

MATLAB - Equivalent logical indexing leading to two different results

I'm writing a piece of code for submission through an online grader, as showcased below. B is some given array filled any/all integers 1 through K, and I want to extract the corresponding logical indices of matrix X and perform some operations on…
rw435
  • 305
  • 2
  • 11
-2
votes
3 answers

For loop and matrix indexing in matlab ques

I have defined a matrix (initial_matrix) equal to 0 by using a for loop as: I = 5; % e.g number of nodes for i =1:I initial_matrix = [0]; // an initial matrix will be generated for each node end Now, for each node i, I will consider all…
Mokujin
  • 65
  • 6
1 2 3
20
21