vectorization

Is there a way to calculate the following specified matrix by avoiding loops? in R or Matlab

。_饼干妹妹 提交于 2020-01-05 10:31:24
问题 I have an N-by-M matrix X , and I need to calculate an N-by-N matrix Y : Y[i, j] = sum((X[i,] - X[j,]) ^ 2) 0 <= i,j <= N For now, I have to use nested loops to do it with O(n 2 ). I would like to know if there's a better way, like using matrix operations. more generally, sum(....) can be a function, fun(x1,x 2) of which x1 , x2 are M-by-1 vectors. 回答1: you can use expand.grid to get a data.frame of possible pairs: X <- matrix(sample(1:5, 50, replace=TRUE), nrow=10) row.ind <- expand.grid(1

NumPy: vectorize sum of distances to a set of points

你离开我真会死。 提交于 2020-01-05 05:43:07
问题 I'm trying to implementing a k-medoids clustering algorithm in Python/NumPy. As part of this algo, I have to compute the sum of distances from objects to their "medoids" (cluster representatives). I have: a distance matrix on five points n_samples = 5 D = np.array([[ 0. , 3.04959014, 4.74341649, 3.72424489, 6.70298441], [ 3.04959014, 0. , 5.38516481, 4.52216762, 6.16846821], [ 4.74341649, 5.38516481, 0. , 1.02469508, 8.23711114], [ 3.72424489, 4.52216762, 1.02469508, 0. , 7.69025357], [ 6

matlab: addressing of one index without sub2ind

故事扮演 提交于 2020-01-05 04:48:47
问题 This is very closely related to this other question, but that question wanted to avoid sub2ind because of performance concerns. I am more concerned about the "unelegance" of using sub2ind . Let's suppose I want to create another MxN matrix which is all zeros except for one entry in each column that I want to assign from the corresponding entry in a vector, and choice of row in each column is based on another vector. For example: z = zeros(10,4); rchoice = [3 1 8 7]; newvals = [123 456 789 10]

matlab: addressing of one index without sub2ind

随声附和 提交于 2020-01-05 04:48:08
问题 This is very closely related to this other question, but that question wanted to avoid sub2ind because of performance concerns. I am more concerned about the "unelegance" of using sub2ind . Let's suppose I want to create another MxN matrix which is all zeros except for one entry in each column that I want to assign from the corresponding entry in a vector, and choice of row in each column is based on another vector. For example: z = zeros(10,4); rchoice = [3 1 8 7]; newvals = [123 456 789 10]

Vectorizing nested loop with conditionals and functions

早过忘川 提交于 2020-01-05 03:54:05
问题 I have the following function: def F(x): #F receives a numpy vector (x) with size (xsize*ysize) ff = np.zeros(xsize*ysize) count=0 for i in range(xsize): for j in range(ysize): a=function(i,j,xsize,ysize) if (a>xsize): ff[count] = x[count]*a else ff[count] = x[count]*i*j count = count +1 return ff There is one nuance here which is the fact that (example for xsize =4, ysize=3) c=count x[c=0] corresponds to x00 i=0,j=0 x[c=1] x01 i=0, j=1 x[c=2] x02 i=0, j=2 (i=0, j = ysize-1) x[c=3] x10 i=1, j

Python - How to generate the Pairwise Hamming Distance Matrix

旧城冷巷雨未停 提交于 2020-01-05 03:49:06
问题 beginner with Python here. So I'm having trouble trying to calculate the resulting binary pairwise hammington distance matrix between the rows of an input matrix using only the numpy library. I'm supposed to avoid loops and use vectorization. If for instance I have something like: [ 1, 0, 0, 1, 1, 0] [ 1, 0, 0, 0, 0, 0] [ 1, 1, 1, 1, 0, 0] The matrix should be something like: [ 0, 2, 3] [ 2, 0, 3] [ 3, 3, 0] ie if the original matrix was A and the hammingdistance matrix is B. B[0,1] =

Vectorizing euclidean distance computation - NumPy

妖精的绣舞 提交于 2020-01-05 01:32:13
问题 my question regards the vectorization of my code. I have one array that holds 3D-coordinates and one array that holds the information of edges that connect the coordinates: In [8]:coords Out[8]: array([[ 11.22727013, 24.72620964, 2.02986932], [ 11.23895836, 24.67577744, 2.04130101], [ 11.23624039, 24.63677788, 2.04096866], [ 11.22516632, 24.5986824 , 2.04045677], [ 11.21166992, 24.56095695, 2.03898215], [ 11.20334721, 24.5227356 , 2.03556442], [ 11.2064085 , 24.48479462, 2.03098583], [ 11

How to vectorize nested for-loops when column j is used as a row number (e.g. mydata[j, “var”])?

淺唱寂寞╮ 提交于 2020-01-04 14:01:36
问题 I am trying to vectorize my code and need some help. Here is a dummy example. a <- c(0, 0, 0) b <- c(0, 0, 0) c <- c(0, 0, 0) rm <- c(1, 2, 3) var1 <- c(100, 101, 102) var2 <- c(200, 201, 202) var3 <- c(300, 301, 302) mat <- data.matrix(cbind(a, b, c, rm, var1, var2, var3)) num <- 3 for (i in 1:num) { for (j in 1:num) { mat[i, j] <- mat[i, 1+num+ mat[j, "rm"]] } } 来源: https://stackoverflow.com/questions/52594597/how-to-vectorize-nested-for-loops-when-column-j-is-used-as-a-row-number-e-g-my

Loop vectorization gives different answer

回眸只為那壹抹淺笑 提交于 2020-01-04 09:38:10
问题 I am building some unit tests and find that my code gives a slightly different result when vectorized. In my example case below, an array a is summed in one dimension and added to an initial value x . Most elements of a are too small to change x . The code is: module datamod use ISO_FORTRAN_ENV, only : dp => REAL64 implicit none ! -- Array dimensions are large enough for gfortran to vectorize integer, parameter :: N = 6 integer, parameter :: M = 10 real(dp) :: x(N), a(N,M) contains subroutine

vectorized indexing/slicing in numpy/scipy?

南楼画角 提交于 2020-01-04 07:51:19
问题 I have an array A, and I have a list of slicing indices (s,t), let's called this list L. I want to find the 85 percentiles of A[s1:t1], A[s2:t2] ... Is there a way to vectorize these operations in numpy? ans = [] for (s,t) in L: ans.append( numpy.percentile( A[s:t], 85) ); looks cumbersome. Thanks a lot! PS: it's safe to assume s1 < s2 .... t1 < t2 ..... This is really just a sliding window percentile problem. 回答1: Given that you're dealing with a non-uniform interval (i.e. the slices aren't