vectorization

How to vectorize a distance calculation using SSE2

回眸只為那壹抹淺笑 提交于 2019-12-12 07:16:32
问题 A and B are vectors or length N, where N could be in the range 20 to 200 say. I want to calculate the square of the distance between these vectors, i.e. d^2 = ||A-B||^2. So far I have: float* a = ...; float* b = ...; float d2 = 0; for(int k = 0; k < N; ++k) { float d = a[k] - b[k]; d2 += d * d; } That seems to work fine, except that I have profiled my code and this is the bottleneck (more than 50% of time is spent just doing this). I am using Visual Studio 2012, on Win 7, with these

Vectorize/Accelerate looping through a struct of struct in Matlab?

 ̄綄美尐妖づ 提交于 2019-12-12 05:47:39
问题 I am looking for something similar to sapply function in R in Matlab. I have the current issue: I have a large struct of size 1000, each one inside is a struct, that is, I have a struct of struct . Each substruct are in the same style, that is, the same fields. I am using a function to do something on each substruct The code looks like: for i =1:length(mainStruct) disp(i); result(i) = myfunction(mainStruct(i).field(1:1000)); end In the above, myfunction is just a function, mainStruct is the

Vectorization of Matlab Code involving ODE solver at each iteration

微笑、不失礼 提交于 2019-12-12 05:15:47
问题 I want to write a fast MATLAB code where I need to write a for loop and I need to solve an ordinary differential equation each time.Is there any way to vectorize the code? Following is the part of the code: tspan=0:0.01:20; dw=rand(p,1); M0=repmat([0 0 1],p,1)'; for p=1:ns [t,M(:,:,p)]=ode45(@(t,M) testfun(t,M,dw(p)),tspan,M0(:,p)); end where function dM=testfun(t,M,w1) M_x=M(1); M_y=M(2); M_z=M(3); dM=[w1*M_y;-w1*M_x+w1*M_z-2*w1*M_y;-w1*M_y-(1-M_z)]; 回答1: Try this and let me know how it

Vectorizing distance calculation between vectors

狂风中的少年 提交于 2019-12-12 04:55:44
问题 I have a 3 X 1000 (and later 3 X 10 000) matrix cord given, which contains the three dimensional coordinates for my pixels. My intention is to calculate the distance between all the pixels, and I do it with a for loop (see below), but I will have to calculate this for huge matrices soon, and am wondering if I could vectorize the code for making it faster...? dist = zeros(size(cord,2),size(cord,2)); for i = 1:size(cord,2) for j = 1:size(cord,2) dist(i,j) = norm(cord(:,i)-cord(:,j)); dist(j,i)

subset in parallel using a list of dataframes and a list of vectors

空扰寡人 提交于 2019-12-12 04:32:45
问题 This works: onion$yearone$id %in% mask$yearone This doesn't: onion[1][1] %in% mask[1] onion[1]['id'] %in% mask[1] Why? Short of an obvious way to vectorize in parallel columns in DF and in memberids (so I only get rows within each year when ids are present in both DF and memberids), im using a for loop, but I'm not being lucky at finding the right way to express the index... Help? Example data: yearone <- data.frame(id=c("b","b","c","a","a"),v=rnorm(5)) onion <- list() onion[[1]] <- yearone

How to do (m,n,k) * (n,k) = (m,k) in matlab?

拥有回忆 提交于 2019-12-12 03:45:58
问题 I have found this relevant question: Multiply columns of a matrix with 2d matrix slices of a 3d matrix in MatLab I have the same problem but in my case m can vary for each slice. Is there a way to do that with mtimesx ? Because m varies, my 3d tensor is stored as a list of cells, each containing a matrix. Also my 2d matrix is rather a list of cells each containing a vector. Is there a way I can do this multiplication without a for loop? 回答1: Since your data is already stored in cellarrays,

How do I create a vectorized version of randsample in Matlab

大憨熊 提交于 2019-12-12 03:24:32
问题 I have a support ( supp_epsilon ) and a probability mass function ( pr_mass_epsilon ) in Matlab, constructed as follows. supp_epsilon=[0.005 0.01; 0.01 0.015; 0.015 0.02; 0.02 0.025]; suppsize_epsilon=size(supp_epsilon,1); pr_mass_epsilon=zeros(suppsize_epsilon,1); mu_epsilon=[0; 0]; sigma_epsilon=[1 0.5; 0.5 1]; pr_mass_epsilon=zeros(suppsize_epsilon,1); for j=1:suppsize_epsilon pr_mass_epsilon(j)=mvnpdf(supp_epsilon(j,:),mu_epsilon.',sigma_epsilon)/sum(mvnpdf(supp_epsilon,mu_epsilon.',sigma

R: nested loop with non-numeric index

杀马特。学长 韩版系。学妹 提交于 2019-12-12 03:22:27
问题 I am a political science student and learning R. I have a problem with a nested loop, one of my indices being non-numeric. I have a data frame pwt containing, for each country in the world (column country ) and each year from 1950 to 2011 (column year ) a number of development indicators, among which is GDP. I would like to add a column that contains the % change in GDP from a year to the next. Here is the error I get: Error in `[<-.factor`(`*tmp*`, iseq, value = numeric(0)): replacement has

Efficient way of appending / extracting list elements

浪子不回头ぞ 提交于 2019-12-12 03:02:01
问题 Suppose I have the following list: rays_all = [np.array(r11, r21, r31, r41), np.array(r12, r22, r32, r42), np.array(r13, r23, r33, r43), np.array(r14, r24, r34, r44)] all the r11, r21, r31, etc are arrays with shape (3L,) (think of it as a vector in 3D space). If I want to extract the (4L,3L) array of np.array(r14, r24, r34, r44) , I just simply use rays_all[-1] . If I want to append a new array of np.array(r15, r25, r35, r45) , I just use rays_all.append . Now I arrange the above vectors

Vectorizing nested if-statements

╄→尐↘猪︶ㄣ 提交于 2019-12-12 02:34:30
问题 Problem I am processing roughly 18 million point data sets at the moment that run through different processes. Over the profile viewer I found out that one of my bottlenecks is this part of code and hence I was wondering if it is possible to vectorize multiple if-statements. Code WA=zeros(size(NB_list_z,1),3); for i=1:size(NB_list_z,1); if (NB_list_z(i,2)==0||NB_list_z(i,3)==0); WA(i,1)=BMLS(NB_list_z(i,1),5); else if (BMLS(NB_list_z(i,3),5)>=COG); WA(i,1)=(BMLS(NB_list_z(i,3),5)+BMLS(NB_list