matrix-indexing

Subsref with cells

那年仲夏 提交于 2019-12-21 17:53:41
问题 This issue appeared when I was answering this question. It should be some stupid error I am doing, but I can't get what error it is… myMatrix = [22 33; 44 55] Returns: >> subsref(myMatrix, struct('type','()','subs',{{[1 2]}} ) ); ans = 22 44 While using it with cells: myCell = {2 3; 4 5} Returns: >> subsref(myCell,struct('type','{}','subs',{{[1 2]}} ) ); ans = 2 % WHATTT?? Shouldn't this be 2 and 4 Matlab?? Checking the subsref documentation, we see: See how MATLAB calls subsref for the

Taking a data.table slice with a sequence of (row,col) indices

孤者浪人 提交于 2019-12-20 05:37:07
问题 I have a data.table that resembles the one below. tab <- data.table(a = c(NA, 42190, NA), b = c(42190, 42190, NA), c = c(40570, 42190, NA)) tab a b c 1: NA 42190 40570 2: 42190 42190 42190 3: NA NA NA Upon specification of a vector of row indices, and a vector of column indices, I would like a vector returned containing the points in tab corresponding to the specified vector of row indices and column indices. For example, suppose I wanted to get the diagonal elements in tab . I would specify

Access matrix value using a vector of coordinates?

随声附和 提交于 2019-12-20 02:29:25
问题 Let's say we have a vector: b = [3, 2, 1]; Let's say we also have matrix like this: A = ones([10 10 10]); I want to use vector b as a source of coordinates to assign values to matrix A . In this example it will be equivalent to: A(3, 2, 1) = 5; Is there an easy way in MALTAB to use a vector as a source of coordinates for indexing a matrix? 回答1: You can do this by converting your vector b into a cell array: B = num2cell(b); A(B{:}) = 5; The second line will expand B into a comma-separated list

Access matrix value using a vector of coordinates?

烈酒焚心 提交于 2019-12-20 02:29:08
问题 Let's say we have a vector: b = [3, 2, 1]; Let's say we also have matrix like this: A = ones([10 10 10]); I want to use vector b as a source of coordinates to assign values to matrix A . In this example it will be equivalent to: A(3, 2, 1) = 5; Is there an easy way in MALTAB to use a vector as a source of coordinates for indexing a matrix? 回答1: You can do this by converting your vector b into a cell array: B = num2cell(b); A(B{:}) = 5; The second line will expand B into a comma-separated list

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

北战南征 提交于 2019-12-19 10:26:43
问题 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 matrix? 回答1: go for idx = logical(ones(size(A))); % // all indices here or, as @Gunther Struyf suggests, idx = true(size(A)); then idx(I) = 0; % // excluding not desired indices B = A(idx); % // selection Alternatively B = A; B(I) = []; 回答2: You

Indexing of unknown dimensional matrix

烈酒焚心 提交于 2019-12-17 12:20:40
问题 I have a non-fixed dimensional matrix M, from which I want to access a single element. The element's indices are contained in a vector J. So for example: M = rand(6,4,8,2); J = [5 2 7 1]; output = M(5,2,7,1) This time M has 4 dimensions, but this is not known in advance. This is dependent on the setup of the algorithm I'm writing. It could likewise be that M = rand(6,4); J = [3 1]; output = M(3,1) so I can't simply use output=M(J(1),J(2)) I was thinking of using sub2ind, but this also needs

Python: Index an array using the colon operator in an arbitrary dimension

回眸只為那壹抹淺笑 提交于 2019-12-12 10:33:50
问题 I have a numpy nd array. A simplified version of my task is to take a vector from along each axis. To illustrate: import numpy x = numpy.array(range(24)).reshape((2,3,4)) x0 = x[0,0,:] x1 = x[0,:,0] x2 = x[:,0,0] However I do not necessarily know the number of dimensions x will have. So the challenge is how to place the colon : indexing operator in a variable location. An example of what such syntax could look like: n = x.ndim ind = list(np.zeros(n)) dim = 0 ind[dim] = ':' y = x[ind] or y =

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

霸气de小男生 提交于 2019-12-12 05:15:44
问题 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 elements. Ex. For x=1, f(x) = 16 mod 9 = 7 For x=2, f(x) = 62 mod 9 = 8 till x = 9 results in permute. permute = [7 8 3 1 2 6 4 5 9]; permute vector gives positions. Using matrix indexing, the positions of elements in row vector B are arranged according to

Multiple indices for numpy array: IndexError: failed to coerce slice entry of type numpy.ndarray to integer

…衆ロ難τιáo~ 提交于 2019-12-11 06:56:11
问题 Is there a way to do multiple indexing in a numpy array as described below? arr=np.array([55, 2, 3, 4, 5, 6, 7, 8, 9]) arr[np.arange(0,2):np.arange(5,7)] output: IndexError: too many indices for array Desired output: array([55,2,3,4,5],[2,3,4,5,6]) This problem might be similar to calculating a moving average over an array (but I want to do it without any function that is provided). 回答1: Here's an approach using strides - start_index = np.arange(0,2) L = 5 # Interval length n = arr.strides[0]

Index A Matrix Using A Vector of Indices in Armadillo LIbrary

帅比萌擦擦* 提交于 2019-12-11 06:25:46
问题 I'm using the Armadillo Cpp code for matrix algebra. I have an Eigenvector matrix E that I want to sort by its eigenvalues in a vector d . mat E; vec d; eig_sym(d,E,Rxx); // Sort indices of eignen values / vectors // based on decreasing real part of eigen values. uvec order = sort_index(-d); // Extract top eigen vectors. E = E(span::all,order(1,nb_sources)); I couldn't find anything related to this kind of indexing in the documentation. Indexing using a vector is such a common requirement