matrix-indexing

numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?

两盒软妹~` 提交于 2021-02-05 07:51:53
问题 Question Why are the numpy tuple indexing behaviors inconsistent? Please explain the rational or design decision behind these behaviors. In my understanding, Z[(0,2)] and Z[(0, 2), (0)] are both tuple indexing and expected the consistent behavior for copy/view. If this is incorrect, please explain, import numpy as np Z = np.arange(36).reshape(3, 3, 4) print("Z is \n{}\n".format(Z)) b = Z[ (0,2) # Select Z[0][2] ] print("Tuple indexing Z[(0,2)] is \n{}\nIs view? {}\n".format( b, b.base is not

numpy - why Z[(0,2)] can be view for some cases and be copy in the others?

孤街醉人 提交于 2021-01-29 17:25:32
问题 Continuation to the question numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?. I got the answer and understood that comma triggering advanced index makes a totally different indexing. The answer also provided a way using __array_interface__ to understand copy/view behavior. However apparently I have not got to the bottom of this part of the answer. A view is returned if the new array can be described with shape, strides and all or part of the original data buffer Because I still

Set a value of a specific column for each row of a matrix

折月煮酒 提交于 2020-07-16 05:49:13
问题 I have a matrix A with m rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a (with m values). That is, I want A_{i,a_i} = 1 . Is there a quick way to do this in Matlab (without a for-loop)? 回答1: I solved it using the sub2ind function: A(sub2ind(size(A), 1:numel(a), a')) = 1 来源: https://stackoverflow.com/questions/20957317/set-a-value-of-a-specific-column-for-each-row-of-a-matrix

Set a value of a specific column for each row of a matrix

情到浓时终转凉″ 提交于 2020-07-16 05:48:09
问题 I have a matrix A with m rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a (with m values). That is, I want A_{i,a_i} = 1 . Is there a quick way to do this in Matlab (without a for-loop)? 回答1: I solved it using the sub2ind function: A(sub2ind(size(A), 1:numel(a), a')) = 1 来源: https://stackoverflow.com/questions/20957317/set-a-value-of-a-specific-column-for-each-row-of-a-matrix

How to remove cell elements that are a subset of a larger element? (Matlab)

房东的猫 提交于 2020-05-15 06:20:26
问题 For example, if I have a cell with arrays listed inside: C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]} I want to output: D = {[1,2,3,4], [4,5,6], [7]} What is the most efficient way to remove cell elements that are already included/subset in another larger element? My existing algorithm loops through each element, compares it to each element in the new cell list and updates the new cell list accordingly but it is extremely slow and inefficient (my original cell contains >200 array elements

Can I slice tensors with logical indexing or lists of indices?

依然范特西╮ 提交于 2020-04-12 09:58:54
问题 I'm trying to slice a PyTorch tensor using a logical index on the columns. I want the columns that correspond to a 1 value in the index vector. Both slicing and logical indexing are possible, but are they possible together? If so, how? My attempt keeps throwing the unhelpful error TypeError: indexing a tensor with an object of type ByteTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTensor as the only argument. MCVE Desired Output C =

find value in array according to the row and column number

风流意气都作罢 提交于 2020-03-06 04:54:28
问题 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 i=1:4 d(i,:)=B(A(i,1),A(i,2)) end d =[21; 87;34;66] 回答1: An alternative to sub2ind is d = B(A(:,1)+ (A(:,2)-1)*size(B,1)); 回答2: Use sub2ind to get linear indices of the required values of B and then use these indices to retrieve those values. d = B

Is there a canonical way of obtaining a 0D numpy subarray?

为君一笑 提交于 2020-02-03 10:02:50
问题 Given a numpy ndarray and an index: a = np.random.randint(0,4,(2,3,4)) idx = (1,1,1) is there a clean way of retrieving the 0D subarray of a at idx ? Something equivalent to a[idx + (None,)].squeeze() but less hackish? Note that @filippo's clever a[idx][...] is not equivalent. First, it doesn't work for object arrays. But more seriously it does not return a subarray but a new array: b = a[idx][...] b[()] = 7 a[idx] == 7 # False 回答1: b = a[idx+(Ellipsis,)] I'm testing on one machine and

Is there a canonical way of obtaining a 0D numpy subarray?

与世无争的帅哥 提交于 2020-02-03 09:59:52
问题 Given a numpy ndarray and an index: a = np.random.randint(0,4,(2,3,4)) idx = (1,1,1) is there a clean way of retrieving the 0D subarray of a at idx ? Something equivalent to a[idx + (None,)].squeeze() but less hackish? Note that @filippo's clever a[idx][...] is not equivalent. First, it doesn't work for object arrays. But more seriously it does not return a subarray but a new array: b = a[idx][...] b[()] = 7 a[idx] == 7 # False 回答1: b = a[idx+(Ellipsis,)] I'm testing on one machine and

Logical Arrays - In an assignment A(I) = B, the number of elements in B and I must be the same

落爺英雄遲暮 提交于 2020-01-24 16:46:13
问题 I have three matrices, A, B and C. When B is larger than A, I want to saturate the value with A. It says that the number of elements in I (which is (B > A)) must be the same as the number of elements in A. I checked below and they are the same. >> A = [5 5 5; 5 5 5; 5 5 5]; >> B = [2 2 2; 2 2 2; 2 2 2]; >> C(B > A) = A In an assignment A(I) = B, the number of elements in B and I must be the same. >> numel(B > A) ans = 9 >> numel(A) ans = 9 >> numel(A>B) ans = 9 It is also strange that this