vectorization

How to have vectorize calculation between a 1D and 2D numpy array with if conditions

和自甴很熟 提交于 2021-01-28 22:47:58
问题 I have a calculation using a 1D and a 2D numpy array. It has two levels of if -conditions. I was able to use np.where to avoid one if -statement and further use the slow list comprehension to iterate through each row. Ideally, I would like to vectorize the whole calculation process. Is it possible? Here is my code: import numpy as np r_base = np.linspace(0, 4, 5) np.random.seed(0) r_mat = np.array([r_base * np.random.uniform(0.9, 1.1, 5), r_base * np.random.uniform(0.9, 1.1, 5), r_base * np

numpy: summing every element of numpy array with every element of another

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 13:01:58
问题 I'm coming to python from Matlab. In Matlab, given two vectors that are not necessarily the same length, they can be added if one is a row vector and one is a column vector. v1 = [1 3 5 7] v2 = [2 4 6]' v1 + v2 ans = 3 5 7 9 5 7 9 11 7 9 11 13 I am trying to produce the same behavior in python given two numpy arrays. Looping first came to mind: import numpy as np v1 = np.array([1,3,5,7]) v2 = np.array([2,4,6]) v3 = np.empty((3,4,)) v3[:] = np.nan for i in range(0,3): v3[i,:] = v1 + v2[i] Is

numpy: summing every element of numpy array with every element of another

大憨熊 提交于 2021-01-28 13:01:51
问题 I'm coming to python from Matlab. In Matlab, given two vectors that are not necessarily the same length, they can be added if one is a row vector and one is a column vector. v1 = [1 3 5 7] v2 = [2 4 6]' v1 + v2 ans = 3 5 7 9 5 7 9 11 7 9 11 13 I am trying to produce the same behavior in python given two numpy arrays. Looping first came to mind: import numpy as np v1 = np.array([1,3,5,7]) v2 = np.array([2,4,6]) v3 = np.empty((3,4,)) v3[:] = np.nan for i in range(0,3): v3[i,:] = v1 + v2[i] Is

numpy: summing every element of numpy array with every element of another

天大地大妈咪最大 提交于 2021-01-28 13:01:12
问题 I'm coming to python from Matlab. In Matlab, given two vectors that are not necessarily the same length, they can be added if one is a row vector and one is a column vector. v1 = [1 3 5 7] v2 = [2 4 6]' v1 + v2 ans = 3 5 7 9 5 7 9 11 7 9 11 13 I am trying to produce the same behavior in python given two numpy arrays. Looping first came to mind: import numpy as np v1 = np.array([1,3,5,7]) v2 = np.array([2,4,6]) v3 = np.empty((3,4,)) v3[:] = np.nan for i in range(0,3): v3[i,:] = v1 + v2[i] Is

numpy: summing every element of numpy array with every element of another

流过昼夜 提交于 2021-01-28 12:57:49
问题 I'm coming to python from Matlab. In Matlab, given two vectors that are not necessarily the same length, they can be added if one is a row vector and one is a column vector. v1 = [1 3 5 7] v2 = [2 4 6]' v1 + v2 ans = 3 5 7 9 5 7 9 11 7 9 11 13 I am trying to produce the same behavior in python given two numpy arrays. Looping first came to mind: import numpy as np v1 = np.array([1,3,5,7]) v2 = np.array([2,4,6]) v3 = np.empty((3,4,)) v3[:] = np.nan for i in range(0,3): v3[i,:] = v1 + v2[i] Is

Compute pairwise element of two 1D array

安稳与你 提交于 2021-01-28 07:59:52
问题 Here is my problem : let's say my two array are : import numpy as np first = np.array(["hello", "hello", "hellllo"]) second = np.array(["hlo", "halo", "alle"]) Now I want to get the matrix of distance between each element of the two arrays so for example my distance function is : def diff_len(string1, string2): return abs(len(string1) - len(string2)) So I I would like to get the matrix : hello hello hellllo hlo result1 result2 result3 halo result4 result5 result6 alle result7 result8 result9

Creating all possible combination of rows in matlab

独自空忆成欢 提交于 2021-01-28 05:34:46
问题 I have a matrix which is 9x10000 size. So rows are R1, R2, upto R9. I want to generate all possible combination of the rows such as [R1;R2] [R1;R3].. [R1;R9] [R1;R2;R3]...[R1;R2;R4]... [R1;R2:R3;R4;..R8] I am currently doing this using for loops. Is there any better way of doing this. 回答1: Basically, counting up the binary from 1 to 2^9-i indicates which rows need to be selected: M=... your matrix S=dec2bin(1:2^size(M,1)-1)=='1'; allSubsets=cell(size(S,1),1); for ix=1:size(S,1) allSubsets{ix}

Is there an intrinsic function to zero out the last n bytes of a __m128i vector?

感情迁移 提交于 2021-01-27 20:54:30
问题 Given n , I want to zero out the last n bytes of a __m128i vector. For instance consider the following __m128i vector: 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 After zeroing out the last n = 4 bytes, the vector should look like: 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00000000 00000000 00000000 Is there a SSE

Vectorizing which operation across the rows of a matrix

戏子无情 提交于 2021-01-27 16:00:31
问题 I would like to vectorize ( apply ) a which operation on matrix X as illustrated by the following for loop having as result the vector ind : X = matrix( 1:20, 4, 5 ) V = sample( 1:20, 4 ) ind = numeric() for( i in 1:nrow(X) ) ind[i] = max( c(0, which(X[i,] < V[i]) )) The operation returns in ind for each row in X the index of the element with the highest value smaller than the value indicated by the X -row-corresponding element of V . The operation max maps the vector of all eligible indices

Numpy array with different standard deviation per row

风格不统一 提交于 2021-01-27 05:50:48
问题 I'd like to get an NxM matrix where numbers in each row are random samples generated from different normal distributions(same mean but different standard deviations). The following code works: import numpy as np mean = 0.0 # same mean stds = [1.0, 2.0, 3.0] # different stds matrix = np.random.random((3,10)) for i,std in enumerate(stds): matrix[i] = np.random.normal(mean, std, matrix.shape[1]) However, this code is not quite efficient as there is a for loop involved. Is there a faster way to