vectorization

Numpy 3darray matrix multiplication function

扶醉桌前 提交于 2019-12-24 00:59:22
问题 Suppose I have an ndarray, W of shape (m,n,n) and a vector C of dimension (m,n). I need to multiply these two in the following way result = np.empty(m,n) for i in range(m): result[i] = W[i] @ C[i] How do I do this in a vectorized way without loops and all? 回答1: Since, you need to keep the first axis from both W and C aligned, while loosing the last axis from them with the matrix-multiplication, I would suggest using np.einsum for a very efficient approach, like so - np.einsum('ijk,ik->ij',W,C

Python mask image pixels from a set of values

你。 提交于 2019-12-24 00:53:41
问题 Provided an image with labels (the value of a pixel corresponds to its label), and the list of labels that are accepted, I am trying to create a "mask" image with 255 value if the pixels label is accepted, 0 otherwise. I know that this is a slow approach as it iterates over the image at python-speed (but it demonstrates the idea well): mask = numpy.zeros(labels.shape[:2], dtype = "uint8") for i in xrange(mask.shape[0]): for j in xrange(mask.shape[1]): if labels[i][j] in accepted: mask[i][j] =

How can I calculate dyadics in matlab without using for loops?

南楼画角 提交于 2019-12-24 00:11:52
问题 I was wondering if someone could help me with my problem. Let say that I have the coordinates of MxN vectors in a tensor r of dimensions [M,N,3]. I would like to save in a 3M-by-3N block matrix all dyadic products r_0'*r_0, where r_0 is the vector r_0 = r(m,n,:) for some m and n, and I would like to do this without using for loops. If haven't explain myself correctly, here is an example code that shows what I would like to obtain (but using for loops, of course): N=10; M=5; r=rand(M,N,3);

Loop vectorization and how to avoid it

[亡魂溺海] 提交于 2019-12-24 00:02:47
问题 Loop vectorization is when all right-hand-side expressions are computed at the onset. I just discovered my loops are being vectorized (in FORTRAN 77... don't ask). I need my loop condition variable to be updated in each iteration, but how can I rewrite to work around this vectorization? In a related post, I'm looking for a way to disable this optimization "feature" in FORTRAN specifically, but here I am looking for a more algorithmic solution to the general case. 回答1: That's not what loop

R: How to provide a vector to the `mean` argument of `rnorm`?

烂漫一生 提交于 2019-12-23 22:11:15
问题 How do I provide a vector to the mean s argument of rnorm ? around_int1_mean <- seq(1.5, 3.5, 0.1) I would like to do something like rnorm(n=25, mean=around_int1_mean, sd=0.2) , avoiding a for -loop. I want to get length(around_int1_mean) sets of samples with n=25 with mean (in the first set) of 1.5, then 1.6 and so on until the last set has mean 3.5. So in the end I'd get 21 sets of samples of size 25. 回答1: I want to get length(around_int1_mean) sets of samples with n=25 with mean (in the

Complex data reorganization with vector instructions

蓝咒 提交于 2019-12-23 22:06:59
问题 I need to load and rearrange 12 bytes into 16 (or 24 into 32) following the pattern below: ABC DEF GHI JKL becomes ABBC DEEF GHHI JKKL Can you suggest efficient ways to achieve this using the SSE(2) and/or AVX(2) instructions ? This needs to be performed repeatedly, so pre-stored masks or constants are allowed. 回答1: By far your best bet is to use a byte shuffle ( pshufb ) . Shifting within elements isn't enough by itself, since JKL has to move farther to the right than DEF , etc. etc. So you

Numpy: Generating a 2D Sum of Gaussians pdf as an array

删除回忆录丶 提交于 2019-12-23 21:25:03
问题 I'm trying to generate a [600 x 600] numpy array that contains the sum of 10 Gaussian-like arrays (each with a randomly-generated center). I've tried using a Gaussian filter to generate the individual Gaussian-like arrays, then summing them up, but I'm sure there's a vectorized way to approach this. Even with num_centers=10 it's slow, and I might need to sum as many as 20 Gaussians. There is a similar question here, but it doesn't seem to have a good or conclusive answer and I'm not sure how

Fast vectorized conversion from RGB to BGRA

我的未来我决定 提交于 2019-12-23 20:14:22
问题 In a follow-up to some previous questions on converting RGB to RGBA, and ARGB to BGR, I would like to speed up a RGB to BGRA conversion with SSE . Assume a 32-bit machine, and would like to use intrinsics . I'm having difficulty aligning both source and destination buffers to work with 128-bit registers, and seek for other savvy vectorization solutions. The routine to be vectorized is as follows... void RGB8ToBGRX8(int w, const void *in, void *out) { int i; int width = w; const unsigned char

Combination of colon-operations in MATLAB

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:25:55
问题 I have a question concerning the colon operator and expansion of vectors in MATLAB. My problem is to understand how the following line of code expands, to be able to use it for other sequences. The line of MATLAB code is: a(1:2:5) = 1:-4:-7 Note that a is not defined before the expansion. This returns the vector a = 1 0 3 0 -7 I know how the colon operator works with {start}:{step}:{stop} , my problem is to understand how and why the combination of a(1:2:5) and 1:-4:-7 returns a vector of

Using a loop (or vectorisation) to subset a list by multiple elements in a vector

风流意气都作罢 提交于 2019-12-23 18:28:46
问题 I have a list of 3 data.frame s: my_list <- list(a = data.frame(value = c(1:5), class = c(letters[1:3],"a", "b")), b = data.frame (value = c(6:1),class=c(letters[1:4],"a", "b")),c=data.frame(value = c(1:7),class = c(letters[5:1],"a", "b"))) my_list $a value class 1 1 a 2 2 b 3 3 c 4 4 a 5 5 b $b value class 1 6 a 2 5 b 3 4 c 4 3 d 5 2 a 6 1 b $c value class 1 1 e 2 2 d 3 3 c 4 4 b 5 5 a 6 6 a 7 7 b I want to go in to each list and subset them by letters a and b from the class column: wanted