matrix-indexing

Cannot assign values to numpy array using 3D masking and indexing

故事扮演 提交于 2019-12-11 03:42:24
问题 I have an 3D array that is a mask. Additionally, I have some indices that encode where (array position) some values should be saved. Everything seems to be working fine, except that the output matrix is still empty after assigning the values to the desired positions. I can not see what I am missing here. I have also tried numpy.put with no luck. import numpy as np # Initialize output matrix (here the results will be stored) results = np.zeros((67, 67, 45)) # define the mask - where to put the

Access predefined elements of cells

冷暖自知 提交于 2019-12-10 10:57:45
问题 I have a cell array A [1x80] in which each element is a cell array itself [9x2]. I have also a vector B representing a group of selected cells of A and I want to extract the element {2,2} of each selected cell. I tried with a simple A(1,B){2,2} but of course it doesn't work.... Can you help me? 回答1: How about this: A = {{1 2; 3 4}, {5 6;7 8}, {9 0; 1 2}; {3 4; 5 6}, {7 8; 9 0}, {11 22; 33 44}}; B = [2,3] [cellfun(@(x)x(2,2), A){1, B}] ans = 8 2 EDIT: The above actually only works in octave.

NumPy: Unsort/undo a reverse/descending sort

烂漫一生 提交于 2019-12-08 10:36:10
问题 I can do an in-place reverse sort (descending sort) of a numpy array, but I also need to be able to unsort (undo) it later. Given an unsorted example: a = np.array([-1, -2, 1, -3, 2, 0]) I tried: i = a[::-1].argsort().argsort() # BAD attempt to store original index # i = array([3, 5, 0, 4, 1, 2]) a[::-1].sort() # in-place reverse sort (works correctly) # a= array([ 2, 1, 0, -1, -2, -3]) a = a[i] # FAILS to restore original a # a = array([-1, -3, 2, -2, 1, 0]) The above doesn't work. What

Getting a grid of a matrix via logical indexing in Numpy

二次信任 提交于 2019-12-07 03:56:46
问题 I'm trying to rewrite a function using numpy which is originally in MATLAB. There's a logical indexing part which is as follows in MATLAB: X = reshape(1:16, 4, 4).'; idx = [true, false, false, true]; X(idx, idx) ans = 1 4 13 16 When I try to make it in numpy, I can't get the correct indexing: X = np.arange(1, 17).reshape(4, 4) idx = [True, False, False, True] X[idx, idx] # Output: array([6, 1, 1, 6]) What's the proper way of getting a grid from the matrix via logical indexing? 回答1: You could

Access predefined elements of cells

笑着哭i 提交于 2019-12-06 07:13:17
I have a cell array A [1x80] in which each element is a cell array itself [9x2]. I have also a vector B representing a group of selected cells of A and I want to extract the element {2,2} of each selected cell. I tried with a simple A(1,B){2,2} but of course it doesn't work.... Can you help me? How about this: A = {{1 2; 3 4}, {5 6;7 8}, {9 0; 1 2}; {3 4; 5 6}, {7 8; 9 0}, {11 22; 33 44}}; B = [2,3] [cellfun(@(x)x(2,2), A){1, B}] ans = 8 2 EDIT: The above actually only works in octave. As @Amro points out, to modify it to work in Matlab you need to use a temporary variable: temp = cellfun(@(x

Numpy re-index to first N natural numbers

懵懂的女人 提交于 2019-12-06 07:10:03
问题 I have a matrix that has a quite sparse index (the largest values in both rows and columns are beyond 130000), but only a few of those rows/columns actually have non-zero values. Thus, I want to have the row and column indices shifted to only represent the non-zero ones, by the first N natural numbers. Visually, I want a example matrix like this 1 0 1 0 0 0 0 0 1 to look like this 1 1 0 1 but only if all values in the row/column are zero. Since I do have the matrix in a sparse format, I could

Numpy 3d array indexing

ぐ巨炮叔叔 提交于 2019-12-05 19:32:49
I have a 3d numpy array ( n_samples x num_components x 2 ) in the example below n_samples = 5 and num_components = 7. I have another array ( indices ) which is the selected component for each sample which is of shape ( n_samples ,). I want to select from the data array given the indices so that the resulting array is n_samples x 2 . The code is below: import numpy as np np.random.seed(77) data=np.random.randint(low=0, high=10, size=(5, 7, 2)) indices = np.array([0, 1, 6, 4, 5]) #how can I select indices from the data array? For example for data 0, the selected component should be the 0th and

Numpy re-index to first N natural numbers

☆樱花仙子☆ 提交于 2019-12-04 13:47:57
I have a matrix that has a quite sparse index (the largest values in both rows and columns are beyond 130000), but only a few of those rows/columns actually have non-zero values. Thus, I want to have the row and column indices shifted to only represent the non-zero ones, by the first N natural numbers. Visually, I want a example matrix like this 1 0 1 0 0 0 0 0 1 to look like this 1 1 0 1 but only if all values in the row/column are zero. Since I do have the matrix in a sparse format, I could simply create a dictionary, store every value by an increasing counter (for row and matrix separately)

Subsref with cells

折月煮酒 提交于 2019-12-04 10:37:55
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 expression: A{1:2} The syntax A{1:2} calls B = subsref(A,S) where S.type='{}' and S.subs={[1 2]}. This

How to extract non-vertical column from matrix in Matlab

余生长醉 提交于 2019-12-04 05:28:58
问题 I have matrix A and a vector b, which specifies column index of the element to be extracted for each corresponding row of the matrix. For example, A = [1 2 3 4 5 6 7 8 9] b = [1 3 2]' I'd like to have c = [1 6 8]' on output. How to achieve this? I tried A(:, b) , but it doesn't work as I need. 回答1: There may be a more elegant solution, but this works: b = [1 3 2]'; [rows, cols] = size(A); A(sub2ind([rows cols], [1 : rows]', b)) 回答2: As an alternative to @dantswain's solution, you can go to