r,for-loop,web-scraping,vectorization,rvest

Probably just a tryCatch() would work here to catch the error and give NA instead. As far as vectorization, I doubt you'd see any real gains. It takes a while (a second or two sometimes) to read a website. With 20K of them, it's gonna take some time. Definitely check...

arrays,matlab,vectorization,reshape,submatrix

You could do - [m,n,r] = size(A); X = P*reshape(A(:,:,1),m*n,[]) If you are doing it iteratively along the third dimension of A, i.e. for A(:, :, iter), where iter is the iterator, you could get all X's in a vectorized manner in an array like so - X_all = P*reshape(A,m*n,[])...

python,c,arrays,numpy,vectorization

One vectorized approach using boolean-indexing/mask - import numpy as np def mask_vectorized(inputs,outputs,pos_y): # Create a copy of outputs array for editing purposes pantilt_2d = outputs[:,:2].copy() # Get mask correspindig to IF conditional statements in original code mask_col0_lt = pantilt_2d[:,0]<-90 mask_col0_gt = pantilt_2d[:,0]>90 # Edit the first column as per the...

Try this, not sure about speed though. Got to run so explanation will have to come later if you need it: interp1(1:nnz(A), A(A ~= 0), cumsum(A ~= 0), 'NearestNeighbor') ...

performance,matlab,matrix,vectorization,nested-loops

Approach #1 With few reshape's and matrix multiplication - A1 = reshape(A,N^3,N)*conj(v) A2 = reshape(A1,N^2,N)*v w = reshape(A2,N,N)*v Approach #2 With one bsxfun , reshape and matrix-multiplication - A1 = reshape(A,N^3,N)*conj(v) vm = bsxfun(@times,v,v.') w = reshape(A1,N,N^2)*vm(:) Benchmarking This section compares runtimes for the two approaches listed in this post,...

r,function,vectorization,apply,mapply

You can try outer f1 <- function(x,y) x^2+x^y-3 outer(1:5, 12:16, f1) which would be similar to t(Vectorize(function(x) f1(x,12:16))(1:5)) ...

Edit: I am redoing the solution because I found out that Matlab does not handle anonymous functions well. So I changed the call from an anonymous function to a normal function. Making this change: Test 1 Comparison(40E3, 3E3) Elapsed time is 21.731176 seconds. Elapsed time is 251.327347 seconds. |y2-y1| =...

matlab,for-loop,nested,vectorization

What about NUM_ITER = 10000; out = zeros(NUM_ITER, 15); for iter = 1:NUM_ITER start=zeros(2,15); a=[-12 10 -5 3 21 19 3 7 17 21]; for u = 1:30 acx = rand(); newacx = round(acx*100); if (newacx < 10 || newacx == 10) [valueone,positionone] = randomFunction(a); elseif (newacx > 10) [valueone,positionone]...

python,arrays,numpy,vectorization,cython

The vectorization of your code would be accomplished if you could pass the whole array ks to the methods self.__gamma() and self.__kappa(), preventing the overhead of function calls for each loop iteration since you would be moving the loop to the inner-most called methods. There are some other tips that...

Your function is not defined to handle vector input. Modify your impluse function as follows: function y=impulse_function(n) [a b]=size(n); y=zeros(a,b); y(n==0)=1; end In your definition of impulse_function, whole array is compared to zero and return value is only a single number instead of a vector....

arrays,vector,scipy,vectorization,linear-algebra

You could use the meshgrid function from numpy: import numpy as np M=10 N=10 D=1 ux=0.5 uy=0.5 xo=1 yo=1 A=np.empty((M,N,3)) x=range(M) y=range(N) xv, yv = np.meshgrid(x, y, sparse=False, indexing='ij') A[:,:,0]=D*ux - (xv-xo) A[:,:,1]=D*uy - (yv-yo) A[:,:,2]=D ...

How about using cummax to compute the running max: cm = cummax(x); ind = [1,find(x(2:end) > cm(1:end-1))+1]; y = x(ind); Adapting the method from Divakar (until Divakar posts it if desired): ind = find([true diff(cummax(x))>0]); y = x(ind); ...

arrays,matlab,matrix,multidimensional-array,vectorization

You can use ndgrid and some re-arrangement later on with reshape + permute to get the desired output - %// Get the vector v values which are rectangular grid data on a 2D space [X,Y] = ndgrid(1:n,1:m) %// Reshape those values and re-arrange into a 2D array as the final...

Matlab as a language does this type of operation poorly - you will always require an outside O(N) loop/operation involving at minimum O(K) copies which will not be worth it in performance to vectorize further because matlab is a heavy weight language. Instead, consider using the filter function where these...

performance,matlab,matrix,vector,vectorization

Approach #1 bsxfun based approach - out = bsxfun(@minus,X,X').^2 + bsxfun(@minus,Y,Y').^2 < radius^2 out(1:n+1:end)= 0 Approach #2 Distance matrix calculation using matrix-multiplication based approach (possibly faster) - A = [X(:) Y(:)] A_t = A.'; %//' out = [-2*A A.^2 ones(n,3)]*[A_t ; ones(3,n) ; A_t.^2] < radius^2 out(1:n+1:end)= 0 Approach #3...

matlab,matrix,vector,vectorization,cross-product

First, make sure you read the documentation of cross very carefully when dealing with matrices: It says: C = cross(A,B,DIM), where A and B are N-D arrays, returns the cross product of vectors in the dimension DIM of A and B. A and B must have the same size, and...

matlab,for-loop,matrix,vectorization,linear-programming

One vectorized approach with bsxfun - A(bsxfun(@plus,ii(:),(jj-1)*size(A,1))) = x You need the expansion with bsxfun as the number of row indices don't match up with the number of column indices. Also, please note that I have replaced the variable names i with ii and j with jj as i and...

Let's look what happens: fac <- function(n) { ifelse(n == 1, 1, {message(paste(n-1, collapse = ",")); stopifnot(n > 0); n * fac(n-1)}) } fac(4:5) #3,4 #2,3 #1,2 #0,1 #-1,0 # Show Traceback # # Rerun with Debug # Error: n > 0 are not all TRUE As you see, the...

For your first question, I'd replace it with following matrix comprehension: nu = (0:nmax)+0.5 bj = [besselj(i,j) for j in x, i in nu] For your second, I think a good principle for writing high performance code in Julia is avoiding unnecessary allocations (and reading the performance tips, of course!)...

matlab,vectorization,cell-array

For your example, you could do the following using the cellfun function: fcnList = {@(x) (x+1), @(x) (x+2)}; a = 2; cellfun(@(func) func(a),fcnList) ans = 3 4 Where I have created a handle called func which accepts as input a function from the fcnList variable. Each function is then evaluated...

matlab,for-loop,vector,vectorization,parfor

You can use a logical vector to index a matrix, as described in the MATLAB help pages. Let's make a simple example: A = [1 2 3 4]; ind = logical([0 1 0 1]); B = A(ind) B = 2 4 You can use this system to model all different...

There really isn't a single answer to your question, but several techniques that you can use as building blocks. Another one you may find helpful: All numpy ufuncs have a .reduceat method, which you can use to your advantage for some of your calculations: >>> a = np.arange(100) >>> breaks...

One can hack into normpdf.m and get all the elements of prob_map in a vectorized manner and thus also avoid those many function calls, which must make it much more efficient. I like to call this hacked approach as using the "raw version" of normpdf's implementation. Here's the final code...

Because of the dependency between iterations to obtain results for each new column with respect to the previous column, it seems you would need at least one loop there, but do all operations within a column in a vectorized fashion and that might speed it up for you. The vectorized...

For efficiency, you could use another vectorized solution with bsxfun alongwith your favourite reshape - Mean_bsxfun = sum(reshape(bsxfun(@times,ch,mask),[],size(ch,3)),1)./sum(mask(:)) Or even better, abuse the fast matrix multiplication in MATLAB - Mean_matmult = mask(:).'*reshape(ch,[],size(ch,3))./sum(mask(:)) ...

You need to cast the second matrix to the form of the first, like this: Eigen::Matrix< float, 3, 1 > mf; Eigen::Matrix< unsigned int, 3, 1 > mi; mf.dot(mi.cast<float>()); Also, Eigen gives ready to use types for vectors, like Eigen::Vector3f for floats and Eigen::Vector3i for ints. (but none for unsigned...

python,numpy,matrix,vectorization

You need to zip the two lists together like so to index the first list by the other. answer = map(lambda x: x[0][x[1]], zip(data, info)) # [2, 0, 4] ...

matlab,matrix,vector,vectorization

You can use the raw version of sub2ind to solve it in a vectorized manner - A( (b(:)-1)*size(A,1) + [1:numel(b)]' ) = 1; How it works: Since elements of b are the column indices and MATLAB follows column-major indexing, so we need to multiply each such column index with the...

matlab,vectorization,accumulate,accumarray

Given: CylCoorsSample = [263.0000 184.2586 10.0000 264.0000 183.0417 10.0000 264.0000 182.1572 10.0000 82.0000 157.4746 11.0000 80.0000 158.2348 11.0000 86.0000 157.3507 11.0000 84.0000 157.7633 11.0000] PointValuesSample = [0.4745 0.5098 0.5020 0.4784 0.4510 0.4431 0.5804] You can use accumarray as follows (using the third output of unique to generate the required subs...

python,performance,numpy,matrix,vectorization

Approach #1 You can use broadcasting as a vectorized approach - import numpy as np Y = np.sum((X - X[:,None,:])**2,2) This should be efficient with relatively smaller input arrays. Approach #2 Seems like you are performing euclidean distance calculations and getting the squared distances. So, you can use distance.cdist like...

ios,arrays,swift,search,vectorization

You can make your dataString array an NSArray and enumerate concurrently. How about something like this? func filterDataByString(dataString:[String], filterString:String) -> [Int] { var arrayIndex = [Int]() let data = dataString as NSArray data.enumerateObjectsWithOptions(.Concurrent) { (string: AnyObject!, index: Int, stop:UnsafeMutablePointer<ObjCBool>) -> Void in if(string as! String == filterString) { arrayIndex.append(index) }...

That is easy for the single array: Just make your sequence a numpy.array and your zigzag function will call C code below the hood. def zigzag(seq): return seq[::2], seq[1::2] seq = np.array([1,1,5,1,5,5,1,5,1,1]) result = zigzag(seq) print(result) Result: (array([1, 5, 5, 1, 1]), array([1, 1, 5, 5, 1])) For the multi...

vectorization,julia-lang,nlopt

Yes, NLopt only understands vectors of decision variables. If your code is more naturally expressed in terms of matrices, then you should convert the vector into a matrix in the function and derivative evaluation callbacks using reinterpret.

python,numpy,multidimensional-array,indexing,vectorization

The complicated slicing operation could be done in a vectorized manner like so - shp = A.shape out = A.reshape(shp[0],shp[1],-1)[np.arange(shp[0]),:,B[:,0]*shp[3] + B[:,1]] You are using the first and second columns of B to index into the third and fourth dimensions of input 4D array, A. With it means is, basically...

python,numpy,vectorization,linear-algebra

You could use scipy.linalg.toeplitz: In [12]: n = 5 In [13]: b = 0.5 In [14]: toeplitz(b**np.arange(n), np.zeros(n)).T Out[14]: array([[ 1. , 0.5 , 0.25 , 0.125 , 0.0625], [ 0. , 1. , 0.5 , 0.25 , 0.125 ], [ 0. , 0. , 1. , 0.5 , 0.25...

matlab,matrix,vectorization,memory-efficient

One vectorized approach - nrows = 4; ncols = 12; row_idx = repmat(1:nrows,ncols/nrows,1) out = bsxfun(@le,[1:nrows]',row_idx(:).') ...

You can use nested outer : set.seed(1) X = rnorm(10) Y = seq(11,20) Z = seq(21,30) F = array(0, dim = c( length(X),length(Y),length(Z) ) ) for (i in 1:length(X)) for (j in 1:length(Y)) for (k in 1:length(Z)) F[i,j,k] = X[i] * (Y[j] + Z[k]) F2 <- outer(X, outer(Y, Z, "+"),...

matlab,vectorization,product,outer-join

With matrix multiplication only: A = A + Z'*diag(hazard)*Z; Note, however, that this requires more operations than Divakar's bsxfun approach, because diag(hazard) is an NxN matrix consisting mostly of zeros. To save some time, you could define the inner matrix as sparse using spdiags, so that multiplication can be optimized:...

That is exactly as the documentation states: "The output type is determined by evaluating the first element of the input, unless it is specified." You can specify the desired output type via the otypes arg, e.g.: np.vectorize(foo, otypes=[np.object]) ...

You can index by a 2-column matrix -- the first column is the row number and the second is the column number. df[cbind(seq(cl), cl)] # [1] 100 310 320 230 140 This is a vectorized operation that should be quicker than looping through the rows with something like sapply and...

performance,matlab,vectorization

%extract a cs list and convert it to a vector, then apply the operation you want in a vectorized manner a=[temp_struct(:).budget]-42 %convert to cell because there is no direct way from vector to cs list a=num2cell(a) %use a cs list to assign the values. [temp_struct(:).budget]=a{:} What is a cs list?...

matlab,matrix,vector,combinations,vectorization

You can generate all possible binary combinations to determine the matrix you want: a = [1 2 3]; n = size(a,2); % generate bit combinations c =(dec2bin(0:(2^n)-1)=='1'); % remove first line c = c(2:end,:) n_c = size(c,1); a_rep = repmat(a,n_c,1); result = c .* a_rep Output: c = 0 0...

python,arrays,numpy,matrix,vectorization

You can do: x = np.ones((5, 5), dtype=float) x[np.triu_indices(5, 1)] = x1 # sets the upper triangle x[np.triu_indices(5, 1)[::-1]] = x1 # sets the lower triangle In the last line, the indices are reversed since your x1 is ordered for the upper triangle. You could also use x[np.tril_indices(5, -1)] =...

python,numpy,vectorization,convolution,probability-density

You can compute the convolution of all your PDFs efficiently using fast fourier transforms (FFTs): the key fact is that the FFT of the convolution is the product of the FFTs of the individual probability density functions. So transform each PDF, multiply the transformed PDFs together, and then perform the...

arrays,matlab,matrix,vectorization,cell-array

The action you want to perform requires an assignment within a function. The only way to achieve this is using eval, which is considered bad practice. A loop is therefore the best remaining option, if you want to keep everything in one script: A = {randn(2,3),randn(2,3)}; for ii = 1:numel(A)...

c++,linux,floating-point,vectorization

Floating point CPU registers can be larger than the floating point type you're working with. This is especially true with float which is typically only 32 bits. A calculation will be computed using all the bits, then the result will be rounded to the nearest representable value before being stored...

openmp,vectorization,simd,powerpc

The XL compiler on POWER Linux currently only supports a subset of the OpenMP 4.0 features. The SIMD construct feature is not supported at the moment, so the compiler will not recognize the construct in your source code. However, if vectorization is what you're looking for then the good news...

One approach with bsxfun - win = zeros(size(data,1),2*shift+1) row_id = 1+shift:size(data,1)-shift win(row_id,:) = data(bsxfun(@plus,row_id(:),[-shift:shift])) Please note that for pre-allocation, you can also use this faster scheme - win(size(data,1),2*shift+1) = 0; ...

performance,matlab,matrix,vectorization

This could be one approach - %// Parameters N = 1024 L = 256 num_seg = 2*(N/L)-1 %// Indices for the first signal with each column representing one segment. %// Thus, there must be 7 columns, representing those 7 segments in a signal. signal1_idx = bsxfun(@plus,[1:L]',[0:num_seg-1]*L/2); %//' %// Indices for...

python,arrays,python-2.7,numpy,vectorization

One approach using mask - import numpy as np # Mask of non-zeros elements in the output array mask = ~np.sort(x==0,2) # Setup output array and put non zero input elements into it out = np.zeros_like(x) out[mask] = x[x!=0] Sample run - Input: In [96]: x Out[96]: array([[[ 0, 2,...

Assuming that A and B are sorted ascending and that B(i) < A(i+1) holds then: idx = zeros(1,max(B)+1); idx(A) = 1; idx(B+1) = -1; C = find(cumsum(idx)) To get around the issue mentioned by Dennis in the comments: m = min(A)-1; A = A-m; B = B-m; idx = zeros(1,max(B)+1);...

r,for-loop,vectorization,portfolio

This uses the function cummax to identify a series of qualifying points by then testing against the original data: > data <- data[order(data$Stdev),] > data[ which(data$AvgReturn == cummax(data$AvgReturn)) , ] Stdev AvgReturn 24 0.81 0.21 19 0.88 0.22 13 0.96 0.24 9 1.05 0.27 5 1.16 0.30 3 1.39 0.31...

r,performance,for-loop,vectorization,rcpp

You can use Rcpp when vectorization is difficult. library(Rcpp) cppFunction(' IntegerVector bin(NumericVector Volume, int n) { IntegerVector binIdexVector(Volume.size()); int binIdex = 1; double totalVolume =0; for(int i=0; i<Volume.size(); i++){ totalVolume = totalVolume + Volume[i]; if (totalVolume <= n) { binIdexVector[i] = binIdex; } else { binIdex++; binIdexVector[i] = binIdex; totalVolume...

matlab,optimization,vectorization

You can do that completely vectorized with logical operators. You can essentially replace that code with: function bottleradius = aux_bottle_radius(z_array) %// Declare initial output array of all zeroes bottleradius = zeros(size(z_array)); %// Condition #1 - Check for all values < -30e-3 and set accordingly bottleradius(z_array < -30e-3) = 34e-3; %//...

matlab,vectorization,computation-theory

Try this: expFun = @(size)exprnd(0.1,size,1); index = [10 20 30 40]; data = cellfun(expFun,mat2cell(index,ones(size(index,1),1),ones(1,size(index,2))),'UniformOutput',false); ...

python,numpy,vectorization,argmax

You could do something like this - # Reshape input array to a 2D array with rows being kept as with original array. # Then, get idnices of max values along the columns. max_idx = A.reshape(A.shape[0],-1).argmax(1) # Get unravel indices corresponding to original shape of A maxpos_vect = np.column_stack(np.unravel_index(max_idx, A[0,:,:].shape))...

In Octave, for finding summations between two vectors, you can use a truly vectorized approach with broadcasting like so - out = reshape(ii(:).' + jj(:),[],1) Here's a runtime test on ideone for the input vectors of size 1 x 100 each - -------------------- With FOR-LOOP Elapsed time is 0.148444 seconds....

performance,matlab,image-processing,vectorization

Approach #1 With bsxfun - %// Get image size [m,n,r] = size(image_rgb) %// Calculate squared distances M = sum(bsxfun(@minus,reshape(image_rgb,[],3),meanv).^2,2) %// Check if sq-ed distance satisfy threshold criteria & reshape back to img size S = reshape(M <= threshold^2 ,m,n) Approach #2 With matrix-multiplication based euclidean distance calculations - %// Get...

python,arrays,performance,numpy,vectorization

This post lists a vectorized approach which basically consists of two steps: Initialize a zeros vector of the same size as input vector, x and set ones at places corresponding to non-zeros of x. Next up, in that vector, we need to put minus of runlengths of each island right...

python,arrays,scipy,vectorization,linear-algebra

You should just divide your array by the sqrt of the sum of squares of your array's last dimension. In [1]: import numpy as np In [2]: x = np.random.rand(1000, 500, 3) In [3]: normed = x / np.sqrt((x**2).sum(axis=-1))[:,:,None] #None could be np.newaxis Note that if you want to compute...

image-processing,vectorization,object-recognition

I am not sure what programming language you are using. Below is a sample i am using in R This is how to use the Pixmap library to read in an image as a matrix. library(pixmap) the next command may only work on Linux system("convert foo.tiff foo.ppm") img <- read.pnm("foo.ppm")...

c++,parallel-processing,vectorization,cpu-cache

You can parallelize your program in two ways: horizontally and vertically. I think you are mixing those two approaches. Horizontal parallelization treats each lane in your SIMD unit as a separate "thread" working on a different data. Vertical parallelization takes whole SIMD unit working on the same data object, trying...

first p(K)=r'*M is wrong (b*y(k,:)' is (2X1) and M is (7X2)) so i think you mean p(k)=r'*M'; then b is (2X3) y is (nX3) r= b*y' is (2Xn) r' is (nX2) M' is (2X7) p=r'*M' is (nX7) so you don't need to use for loop and can simply write: p=(b*y')'M'...

I like to add another answer: sg <- function() { # cutOff should be lower than the lowest of Sepal.Length & Sepal.Width m <- pmin(iris$Sepal.Length, iris$Sepal.Width) ms <- sort.int(m) # use `findInterval` to find all the indices # (equal to "how many numbers below") lower than the threshold plotOutput[,"x"] <-...

You need otypes=[np.int,np.int]: triv_vec = np.vectorize(triv, otypes=[np.int,np.int]) print triv_vec([1,2],[3,4]) (array([1, 2]), array([3, 4])) otypes : str or list of dtypes, optional The output data type. It must be specified as either a string of typecode characters or a list of data type specifiers. There should be one data type specifier...

I think you can try this, Thanks for @JacobH comment, this will be faster. x <- c(0,0,1,0,1,1,0) zeros <- which(x > 0) x[zeros[1]:tail(zeros, n = 1)] the output [1] 1 0 1 1 ...

One way to do it: a = [1 2 3 4] n = numel(a); %// create circulant matrix from input vector b = gallery('circul',[a zeros(1,n-1)]).' %' %// crop the result c = b(:,1:n) Another way: b = union( tril(toeplitz(a)), triu(toeplitz(fliplr(a))),'rows','stable') or its slightly variation b = union( toeplitz(a,a.*0),toeplitz(fliplr(a),a.*0).','rows','stable') and probably...

All you trying to do is accumulate the cost by index. Thus, you might want to use the by argument as in example[, accumulating_cost(threshold, target), by = index] ...

python,arrays,performance,numpy,vectorization

You can use an approach based on np.einsum and matrix-multiplication with np.dot as listed below - # Calculate "((a + b * np.dot(tab[i], vectors[n])) ** d)" part p1 = (a + b*np.einsum('ij,kj->ki',tab,vectors))**d # Include "+= coef[0][n] *" part to get the final output y_vectorized = np.dot(coef,p1) Runtime test Dataset #1:...

matlab,parallel-processing,vectorization

For b and c of length 4 each, when the loop is unrolled, you would have - output = b1b2b3b4 + c1b2b3b4 + c2b3b4 + c3b4 + c4 So, the generic formula would be : output = b1b2b3...bN + c1b2b3..bN + c2b3..bN + c3b4..bN + ... cN-1bN + cN The...

python,numpy,matrix,vectorization

This reproduces your example. It can be generalized to other k and d In [12]: a=np.arange(6) In [13]: b=np.zeros((6,3)) In [14]: b[np.arange(6),np.arange(3).repeat(2)]=a In [15]: b Out[15]: array([[ 0., 0., 0.], [ 1., 0., 0.], [ 0., 2., 0.], [ 0., 3., 0.], [ 0., 0., 4.], [ 0., 0., 5.]])...

arrays,matlab,unique,element,vectorization

Try this sum(diff(sort(y))~=0)+1 which uses functions that do vectorise. Performance seems better than your for loop for your case, however I'd imagine for larger problems my proposed solution would get worse due to memory limitation. With N=1e5, your method takes ~7.5s on my computer, and my proposal takes ~0.05s. With...

python,numpy,scikit-learn,vectorization

np.vectorize does make some improvement in speed - about 2x (here I'm using math.atan2 as an black box function that takes 2 scalar arguments). In [486]: X=np.linspace(0,1,100) In [487]: K_vect=np.vectorize(math.atan2) In [488]: timeit proxy_kernel(X,X,math.atan2) 100 loops, best of 3: 7.84 ms per loop In [489]: timeit K_vect(X[:,None],X) 100 loops, best...

matlab,optimization,vectorization,nested-loops,pdist

You can use efficient euclidean distance calculation as listed in Speed-efficient classification in Matlab for a vectorized solution - %// Setup the input vectors of real and imaginary into Mx2 & Nx2 arrays A = [real(InitialPoints) imag(InitialPoints)]; Bt = [real(newPoints).' ; imag(newPoints).']; %// Calculate squared euclidean distances. This is one...

matlab,matrix,multidimensional-array,vectorization,reshape

Just do the reverse of what you used to reshape the original array. The permute commands stay the same (switching the first and second dimension), while the reshape commands go back up to 512 reshaped_i_image = reshape(permute(reshape(permute(sub_images, [2 1 3]), 8, 512, []), [2 1 3]), 512, 512); ...

r,vectorization,dplyr,zoo,rollapply

Give this a try: DF <- read.table(text = "Name EventType EventDate SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 John Sale 2/1/2014 10 10 10 John Sale 7/1/2014 20 30 30 John Sale 4/1/2015 30 60 50 John Webinar 5/1/2015 0 60 50 Tom Email 1/1/2014 0 0 0...

python,arrays,numpy,vectorization,cartesian-product

Two more approaches could be suggested here. Using matrix-multiplication with np.dot: np.dot(x[:,None],y[None]).ravel() With np.einsum: np.einsum('i,j->ij',x,y).ravel() Runtime tests In [31]: N = 10000 ...: x = np.random.rand(N) ...: y = np.random.rand(N) ...: In [32]: %timeit np.dot(x[:,None],y[None]).ravel() 1 loops, best of 3: 302 ms per loop In [33]: %timeit np.einsum('i,j->ij',x,y).ravel() 1 loops,...

r,parallel-processing,vectorization

Okay, now that I know you have to work around phrases and words... here's another shot at it. Basically, you have to split out your phrases first, score them, remove them from the string, then score your words... library(stringr) sent <- data.frame(words = c("great just great right size and i...

You could just use normal vector indexing within the loop to accomplish this: vector <- numeric(length=100) for (i in 1:10) { vector[(10*i-9):(10*i)] <- 1:10 } all.equal(vector, rep(1:10, 10)) # [1] TRUE Of course if you were just trying to repeat a vector a certain number of times rep(vec, 10) would...

One approach with braodcasting and boolean indexing - import numpy as np # Mask of elements from J to be summed up mask = np.arange(J.shape[0])[:,None] >= JTildeIDX # Map the mask onto input array and sum along columns for final output out = (J*mask).sum(0) Sample run - In [154]: J...

multidimensional-array,scipy,vectorization,linear-algebra,least-squares

You can gain some speed by making use of the stack of matrices feature of numpy.linalg routines. This doesn't yet work for numpy.linalg.lstsq, but numpy.linalg.svd does, so you can implement lstsq yourself: import numpy as np def stacked_lstsq(L, b, rcond=1e-10): """ Solve L x = b, via SVD least squares...

r,datetime,vector,vectorization

I think this could be enough: resize <- function (input, len) approx(seq_along(input), input, n = len)$y For example: > resize(0:4, 10) [1] 0.0000000 0.4444444 0.8888889 1.3333333 1.7777778 2.2222222 2.6666667 3.1111111 3.5555556 4.0000000 > resize( c(0, 3, 2, 1), 10) [1] 0.000000 1.000000 2.000000 3.000000 2.666667 2.333333 2.000000 1.666667 1.333333 1.000000...

Short answer: Yes, the different symbol makes the meaning more clear to the reader. Thanks for this interesting question! If I can summarize, it seems to be a follow-up specifically about this section of my answer to the question you linked, ... you want to use the long forms only...

r,loops,matrix,vectorization,apply

To make my remarks in comment column clear, suppose we have dfmat as a list of matrices. It is almost always easier to work with a list of matrices than one big named matrix. Also if you want to fully vectorize the solution given here, you might want to obtain...

A fully vectorized approach is certainly possible using bsxfun to cover all iterations and all IF conditional statements in one go. Here's one such implementation - %// Collect the relevant column data from input array n = 2; d1n = data(:,1:n); d4 = data(:,4); %// Logical array corrresponding to starting...

r,simulation,vectorization,lapply

It's true that a <- t1() works but it's not true that a <- t1(2) would have "worked". You are trying to pass arguments to parameters that are not there. Put a dummy parameter in the argument list and all will be fine. You might also look at the replicate...

One approach - %// 3d mask of elements greater than 5 mask = m3d>5 %// Sum of all elements greater than 5 in each slice sumvals = sum(reshape(m3d.*mask,[],size(m3d,3))) %// Count of elements great than 5 in each slice counts = sum(reshape(mask,[],size(m3d,3))) %// Final output of mean values for the regions...

It looks like you're trying to grab summary functions from each entry in a list, ignoring the elements set to -999. You can do this with something like: get_scalar <- function(name, FUN=max) { sapply(mydata[,name], function(x) if(all(x == -999)) NA else FUN(as.numeric(x[x != -999]))) } Note that I've changed your function...

You could use addition and broadcasting: >>> x = np.array([1,2,3,4,5]) >>> constant = 3 >>> x[:,None] + np.arange(constant) array([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]) This could also be written as np.add.outer(x, np.arange(constant)). ...

r,performance,matlab,vectorization,reshape

The first step is to convert your array w from 6x9 to 3x3x6 size, which in your case can be done by transposing and then changing the dimension: neww <- t(w) dim(neww) <- c(sqrt(somPEs), sqrt(somPEs), inputPEs) This is almost what we want, except that the first two dimensions are flipped....

python,arrays,numpy,vectorization

This could be one approach - import numpy as np # Parameters R = 3 # Radius M1,N1 = padded.shape rowlen = N1 - R + 1 collen = M1 - R + 1 # Linear indices for the starting R x R block idx1 = np.arange(R)[:,None]*N1 + np.arange(R) #...