elementwise-operations

Element-wise matrix multiplication for multi-dimensional array

有些话、适合烂在心里 提交于 2019-12-18 09:08:56
问题 I want to realize component-wise matrix multiplication in MATLAB, which can be done using numpy.einsum in Python as below: import numpy as np M = 2 N = 4 I = 2000 J = 300 A = np.random.randn(M, M, I) B = np.random.randn(M, M, N, J, I) C = np.random.randn(M, J, I) # using einsum D = np.einsum('mki, klnji, lji -> mnji', A, B, C) # naive for-loop E = np.zeros(M, N, J, I) for i in range(I): for j in range(J): for n in range(N): E[:,n,j,i] = B[:,:,i] @ A[:,:,n,j,i] @ C[:,j,i] print(np.sum(np.abs(D

numpy elementwise outer product

一世执手 提交于 2019-12-17 07:53:38
问题 I want to do the element-wise outer product of two 2d arrays in numpy. A.shape = (100, 3) # A numpy ndarray B.shape = (100, 5) # A numpy ndarray C = element_wise_outer_product(A, B) # A function that does the trick C.shape = (100, 3, 5) # This should be the result C[i] = np.outer(A[i], B[i]) # This should be the result A naive implementation can the following. tmp = [] for i in range(len(A): outer_product = np.outer(A[i], B[i]) tmp.append(outer_product) C = np.array(tmp) A better solution

Element-wise array replication according to a count [duplicate]

和自甴很熟 提交于 2019-12-17 04:35:42
问题 This question already has answers here : Repeat copies of array elements: Run-length decoding in MATLAB (5 answers) Closed 5 years ago . My question is similar to this one, but I would like to replicate each element according to a count specified in a second array of the same size. An example of this, say I had an array v = [3 1 9 4] , I want to use rep = [2 3 1 5] to replicate the first element 2 times, the second three times, and so on to get [3 3 1 1 1 9 4 4 4 4 4] . So far I'm using a

Element-wise array replication according to a count [duplicate]

南楼画角 提交于 2019-12-17 04:35:11
问题 This question already has answers here : Repeat copies of array elements: Run-length decoding in MATLAB (5 answers) Closed 5 years ago . My question is similar to this one, but I would like to replicate each element according to a count specified in a second array of the same size. An example of this, say I had an array v = [3 1 9 4] , I want to use rep = [2 3 1 5] to replicate the first element 2 times, the second three times, and so on to get [3 3 1 1 1 9 4 4 4 4 4] . So far I'm using a

ValueError when defining a lambda function in python

梦想的初衷 提交于 2019-12-13 10:38:17
问题 I am receiving a ValueError when using integration, but I cannot understand why. Here is my simplified code: import numpy as np import scipy.integrate as integrate pbar = 1 p = np.arange(0,pbar,pbar/1000) h = lambda p: p**2/2+p*(1-p) Kl = lambda p: h(p) +0.02 K = Kl(p) R = 0.5*h(p) + 0.5*h(pbar) Vl = lambda p: np.minimum.reduce([p, K, R]) integrate.quad(Vl, 0, pbar)[0] Vl is the element-wise minimum of the three arrays. The last line gives the exception: ValueError: setting an array element

Sparse matrix multiplication in MATLAB with spfun

主宰稳场 提交于 2019-12-13 00:49:02
问题 I have a dense column matrix y of size (m,1) and a sparse matrix x of size (m,n) . I want to do element-wise multiplication using y and every column of x . The resultant sparse matrix is still of size (m,n) . Sparse matrix x , when loaded into memory, is about 10GB. Can spfun help me accomplish my goal in a memory efficient manner? I am having difficulties understanding the logic behind it. Thank you. 回答1: Have you tried bsxfun? out = bsxfun( @times, x, y ); spfun is more suitable for element

inconsistent results using isreal

限于喜欢 提交于 2019-12-12 08:05:57
问题 Take this simple example: a = [1 2i]; x = zeros(1,length(a)); for n=1:length(a) x(n) = isreal(a(n)); end In an attempt to vectorize the code, I tried: y = arrayfun(@isreal,a); But the results are not the same: x = 1 0 y = 0 0 What am I doing wrong? 回答1: This certainly appears to be a bug, but here's a workaround: >> y = arrayfun(@(x) isreal(x(1)),a) ans = 1 0 Why does this work? I'm not totally sure, but it appears that when you perform an indexing operation on the variable before calling

Elementwise logical comparison of numpy arrays

早过忘川 提交于 2019-12-11 09:27:36
问题 I have two numpy arrays of the same shape. The elements in the arrays are random integers from [0,N]. I need to check which (if any) of the elements in the same position in the arrays are equal. The output I need are the positions of the same elements. mock code: A=np.array([0,1]) B=np.array([1,0]) C=np.array([1,1]) np.any_elemenwise(A,B) np.any_elemenwise(A,C) np.any_elemenwise(A,A) desired output: [] [1] [0,1] I can write a loop going through all of the elements one by one, but I assume

Using the values of a previous “row” in a pandas series

只谈情不闲聊 提交于 2019-12-07 06:34:55
问题 I have a CSV that looks like this (and when brought into a pandas Dataframe with read_csv() , it looks the same). I want to update the values in column ad_requests according to the following logic: For a given row, if ad_requests has a value, leave it alone. Else, give it a value of the previous row's value for ad_requests minus the previous row's value for impressions . So in the first example, we would like to end up with: I get partially there: df["ad_requests"] = [i if not pd.isnull(i)

elementwise combination of two lists in R

丶灬走出姿态 提交于 2019-12-06 19:21:06
问题 Say, I have two lists: list.a <- as.list(c("a", "b", "c")) list.b <- as.list(c("d", "e", "f")) I would like to combine these lists recursively, such that the result would be a list of combined elements as a vector like the following: [[1]] [1] a d [[2]] [1] a e [[3]] [1] a f [[4]] [1] b d and so on. I feel like I'm missing something relatively simple here. Any help? Cheers. 回答1: expand.grid(list.1, list.b) gives you the desired result in a data.frame structure. This tends to be the most