vectorization

effective way of transformation from 2D to 1D vector

人走茶凉 提交于 2019-12-25 16:57:20
问题 i want to create 1D vector in matlab from given matrix,for this i have implemented following algorithm ,which use trivial way % create one dimensional vector from 2D matrix function [x]=one_dimensional(b,m,n) k=1; for i=1:m for t=1:n x(k)=b(i,t); k=k+1; end end x; end when i run it using following example,it seems to do it's task fine b=[2 1 3;4 2 3;1 5 4] b = 2 1 3 4 2 3 1 5 4 >> one_dimensional(b,3,3) ans = 2 1 3 4 2 3 1 5 4 but generally i know that,arrays are not good way to use in matlab

Intel Fortran vectorisation: vector loop cost higher than scalar

烈酒焚心 提交于 2019-12-25 16:24:33
问题 I'm testing and optimising a legacy code with Intel Fortran 15, and I have this simple loop: do ir=1,N(lev) G1(lev)%D(ir) = 0.d0 G2(lev)%D(ir) = 0.d0 enddo where lev is equal to some integer. Structures and indexes are quite complex for the compiler, but it can succeed in the task, as I can see on other lines. Now, on the above loop, I get this from the compilation report: LOOP BEGIN at MLFMATranslationProd.f90(38,2) remark #15399: vectorization support: unroll factor set to 4 remark #15300:

Disable vectorized looping in FORTRAN?

风流意气都作罢 提交于 2019-12-25 09:33:58
问题 Is it possible to bypass loop vectorization in FORTRAN? I'm writing to F77 standards for a particular project, but the GNU gfortran compiles up through modern FORTRANs, such as F95. Does anyone know if certain FORTRAN standards avoided loop vectorization or if there are any flags/options in gfortran to turn this off? UPDATE: So, I think the final solution to my specific problem has to "DO" with the FORTRAN DO loops not allowing the updating of the iteration variable. Mention of this can be

Vectorization of a nested for-loop

不打扰是莪最后的温柔 提交于 2019-12-25 08:46:17
问题 I have a function, roughness that is called quite often in a larger piece of code. I need some help with replacing this double for-loop with a simpler vectorized version. Here is the code below: def roughness(c,d,e,f,z,ndim,half_tile,dx): imin=0-half_tile imax=half_tile z_calc = np.zeros((ndim,ndim), dtype=float) for j in range(ndim): y=(j-half_tile)*dx for i in range(ndim): x=(i-half_tile)*dx z_calc[i,j] = c*x*y + d*x + e*y + f - z[i,j] z_min=z_calc[z_calc!=0].min() z_max=z_calc[z_calc!=0]

Theano row/column wise subtraction

怎甘沉沦 提交于 2019-12-25 08:14:54
问题 X is an n by d matrix, W is an m by d matrix, for every row in X I want to compute the squared Euclidean distance with every row in W , so the results will be an n by m matrix. If there's only one row in W , this is easy x = tensor.TensorType("float64", [False, False])() w = tensor.TensorType("float64", [False])() z = tensor.sum((x-w)**2, axis=1) fn = theano.function([x, w], z) print fn([[1,2,3], [2,2,2]], [2,2,2]) # [ 2. 0.] What do I do when W is a matrix (in Theano)? 回答1: Short answer, use

Vectorizing a loop through lines of data frame R while accessing multiple variables the dataframe

不问归期 提交于 2019-12-25 07:49:14
问题 Yet another apply question. I've reviewed a lot of documentation on the apply family of functions in R (and use them quite a bit in my work). I've defined a function myfun below which I want to apply to every row of the dataframe inc . I think I need some variant of apply(inc,1,myfun) I've played around with it for a while, but still can't quite get it. I've included a loop which achieves exactly what I want to do... it's just super slow and inefficient on my real data which is considerably

Is this code correct to allocate two aligned cv::Mat?

我的未来我决定 提交于 2019-12-25 07:41:43
问题 Disclamer: I am a SIMD newbie, but I have a good knowledge of OpenCV. Let's suppose I have basic OpenCV code. cv::Mat1f a = cv::Mat(n, n); cv::Mat1f b = cv::Mat(n, n); cv::Mat1f x; //fill a and b somehow x = a+b; Now, let's suppose I want to use the code above on AVX2 or even AVX-512 machine. Having the data 32-aligned could be a great benefit for performances. The data above should not be aligned. I'm almost certain of it because in the optimization report files generated by the Intel

special add in matlab [duplicate]

限于喜欢 提交于 2019-12-25 05:18:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to Add a row vector to a column vector like matrix multiplication I have a nx1 vector and a 1xn vector. I want to add them in a special manner like matrix multiplication in an efficient manner (vectorized): Example: A=[1 2 3]' B=[4 5 6] A \odd_add B = [1+4 1+5 1+6 2+4 2+5 2+6 3+4 3+5 3+6 ] I have used bsxfun in MATLAB, but I think it is slow. Please help me... 回答1: As mentioned by @b3. this would be an

Sum of product each row by a matrix

爷,独闯天下 提交于 2019-12-24 20:53:48
问题 I have a matrix A and a three-dims matrix B . I want to sum (by i ) A(i,:)*B(i,:,:) , but without loops by i . 回答1: I'll start by creating some random matrices similar to what you described: n = 4; m =3; A = rand(n,m); B = rand(n,m,5); 1) loop version: C = zeros(1,size(B,3)); for i=1:n C = C + A(i,:)*squeeze(B(i,:,:)); end basically it performs matrix multiplication of each row of A by the corresponding slice of B , and accumulates the sum. This is could be slighty improved by permuting the

Appending values to an array within an object (looping over objects)

纵然是瞬间 提交于 2019-12-24 20:14:57
问题 This is an uncluttered version of this question. Since I changed so much I made a new question I am trying to take certain values from a longer array solution and put them into a smaller array, within an object. This code is supposed to take the first half of the solution array and put it into x_hist within m1 , and the second half of the solution array and put it into x_hist within m2 . Instead it appears to take all of the solution array and put it into x_hist for both objects. Anyone know