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

Subvectors from vector using indexes, matlab

I have a vector, F0, with values I need to study. The vector f0 can be separated into sections A&B, A_starts has the indexes where sections of A start and A_ends has the indexes where A ends, the resulting elements of F0 are B. How can I extract the…
0
votes
3 answers

find value in array according to the row and column number

I have two matrix A and B,the first row of matrix A(1,:)=[1 2] refer to the number of row and column matrix B(1,2)=21,now I want to do this work for another rows of matrix A without loops? A=[1 2;2 3;1 3;3 3]; B=[1 21 34;45 65 87;4 55 66]; for…
abbas
  • 57
  • 6
0
votes
1 answer

How to use a cell array directly returned from a function?

I have a path (without a file name at the end) as a string in Matlab and i want to receive the first parent directory (the directory after the last file seperator character) in it. At the moment i'm doing it like this: >>filePath =…
Semih SÜZEN
  • 163
  • 1
  • 2
  • 14
0
votes
2 answers

Assigning values to a matrix through vector addition from an adjacency matrix

Very new to Matlab, I usually use STATA. I want to use the nchoosek fuction to get the sum of vectors in one matrix. I have a 21x21 adjacency matrix, with either 0 or 1 as the inputs. I want to create a new matrix, that will give me a sum of…
0
votes
0 answers

2D array indexing in python

tau = 0.5 dt = 0.01 Nt = int(tau/dt) grid_size = 100 dx = 1/grid_size x = np.linspace(0, grid_size, grid_size) t = np.linspace(dt, tau, Nt+1) Sw_g = [[0 for x1 in range(grid_size)] for y in range(Nt)] Sw_g[:Nt][grid_size-1] = 0.2 #…
0
votes
1 answer

Tensorflow Error when indexing a 4D array: ValueError: Shapes must be equal rank, but are 1 and 0

I'm modifying a simple CNN in Tensorflow and when I'm indexing a 4d array I get this error. My reproducable example is: from __future__ import print_function import pdb import numpy as np import tensorflow as tf from…
Ash
  • 3,428
  • 1
  • 34
  • 44
0
votes
1 answer

How to find the index of the max/min element on a list?

I have a multidimentional list called dist. I want to get the position of the max element on this list/ matrix without using NumPy. How can I do it with built-in functions? I am using Python 3.6 dist = [[ 0.0, 1804.04, …
feys
  • 1
  • 1
0
votes
1 answer

I have a A = 3×3 matrix in which position of elements are to be shuffled based on polynomial function result

A = [1 2 3; 4 5 6; 7 8 9]; Now reshaping the matrix A to form a row vector gives B. B = [1 4 7 2 5 8 3 6 9]; Evaluating polynomial function f(x) = (7x+6x^2+3x^3)mod 9 by putting values for 'x' ranging from (1,...,9) since there are 9…
mayfly
  • 1
  • 2
0
votes
1 answer

Remove rows in matrix where column equals any value in vector

I have a matrix A that is size 967874 x 3 and a vector v that is length 7211. I want to create a new matrix B that equals matrix A minus all the rows in A where the 2nd column value equals any of the elements in v. What's a fast way to accomplish…
Austin
  • 6,921
  • 12
  • 73
  • 138
0
votes
0 answers

accessing indices in two dimensions

I have a vector a and a second vector b to access a range of indices in a, all working as I would expect: a=1:5400; % a==[1 2 3 4 ...] value: (1x5400 double) b=[1 2 3]; % b value: (1x3 double) a(b); % returns [1 2 3] however if a b is a 2D matrix…
user2305193
  • 2,079
  • 18
  • 39
0
votes
1 answer

selecting rows from 2 matrices using an indicator vector in numpy

Suppose I have a vector: f = np.array([1,1,0,0]) #(4,) and 2 matrices: m1 = np.array([[1,2],[3,4],[5,6],[7,8]]) #(4,2) m2 = np.array([[10,20],[30,40],[50,60],[70,80]]) #(4,2) How can I create a new matrix m3 that selects rows from m1 where f == 1…
A.D
  • 1,480
  • 2
  • 18
  • 33
0
votes
1 answer

Get bin edges from a histogram by index of the bin

I have a matrix m and plot a histogram of the third column. I search for the peak in the first 100 bins and get the frequency as a and the index of the bin as b. Now I need the edges of the bin with index b. How can I get them? nbins = 1000; histo…
Chaostante
  • 37
  • 6
0
votes
1 answer

How to select a submatrix in Matlab?

Hello and happy new year! I have a problem, and I can't find a solution. I need to create these 2 matrices, C from B with submatrix. B = [ 1 2 3 4 5 6 7 9 7 5 3 1 -1 -3 4 8 16 32 64 128 256]; And I…
BillΤzik
  • 13
  • 3
0
votes
0 answers

Values from elements that creates concentric square on distance "k" in NumPy ndarray

What is the fastest way to get values from elements that creates concentric square on some distance K from the center. I wrote code where I access every element value with numpy.ndarray.item, but it has shown poor results. To help you visualize…
0
votes
1 answer

solving of zero indexing issue in matlab

suppose that we have some vector y in matlab and let us suppose that there is such linear relationship between values of y of course first vector can be easily implemented by y(L:N-1), but related to matrix, we dont have index y[0] in matlab,…
user466534