FAQ Database Discussion Community

## How to generalize mapply to work “crosswisely”?

r,function,vectorization,apply,mapply
Mapply applies a 2-dimensional function to the 1st elements of each m-dimensional vector, and then to the 2nd elements of each, etc. The result is an m-dimensional vector. For example > mapply(sum, 1:5, 12:16) [1] 13 15 17 19 21 Now, is there a DIRECT alternative to mapply that applies...

## Create a matrix by sliding down a given vector by one step for every column

matlab,matrix,vectorization
Given this vector a = [1 2 3 4] I want to create a matrix like this b = [1 0 0 0; 2 1 0 0; 3 2 1 0; 4 3 2 1; 0 4 3 2; 0 0 4 3; 0 0 0 4] in a vectorized...

## Vectorizing a function of a class with two arrays as inputs in cython

python,arrays,numpy,vectorization,cython
I am struggling with the problem of optimizing my cython code in order to improve its speed as much as possible. One of the challenges that I could not still figure out how it should be done in cython is mapping an array on a function like what is done...

## Evenly spaced numbers between two sets (Vectorize LINSPACE) - MATLAB

matlab,matrix,vector,vectorization
How can I define a matrix M according to M=[a:(b-a)/5:b] (from a to b in 5 steps), when a and b are vectors or sets; more specifically, each row i in M should have a first value equal to a(i) and last value b(i) and, in between, 5 equal steps....

## Vectorize columns replacement based on a vector - MATLAB

matlab,matrix,vector,vectorization
I want to replace value of n-th column of a matrix, based on a vector values, in a vectorized way. Input: [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0] Vector: [2] [4] [1] [3] Expected output: [0 1 0 0] [0 0...

## Eigen: Operating on Vectors of Different Types

c++,vectorization,eigen
I have many expressions that look like auto result = vec3f.cwiseProduct( vec3ui ); where vec3f is from type Eigen::Matrix< float, 3, 1 > and vec3ui is from Eigen::Matrix< unsigned int, 3, 1 >. These doesn't seem to be allowed, at least the compiler complains about it. Hence I need to...

## Integrate a function that has a function as a parameter in R

r,function,parameters,vectorization
So I just answered a question and as soon as it was answered (correctly I think), the questioner deleted it. So here it is again: I am new to R and need help getting this function to work. I need to create a function that can find the log of...

## vectorization of two for loop (matrix creation and thresholding)

matlab,vectorization
How to vectorize this MATLAB function: function [win,vec] = createwin(data,shift,regions,threshold) % % data ... 1D data array % shift ... positive integer scalar % regions ... integer index vector % threshold ... integer scalar % win = zeros(length(data),2*shift+1); for i = 1+shift:length(data)-shift win(i,:) = data(i-shift:i+shift); end vec = false(size(data)); for...

## Generalize stacking of array elements' neighbors into 3-D array

python,arrays,numpy,vectorization
Setup Given a 2D array, I would like to create a 3D array where the values along the third dimension at (i.e. stacked[row, col, :]) are the flattened neighbors of the original array at [row, col]. I would like to generalize this process to handle an arbitrary (but reasonable) search...

## How to get mean values of certain regions in each slice in 3d matrix using logical indexing MATLAB

matlab,matrix,vectorization
In Matlab, If I have a 3d matrix as following, I want to know the mean of the regions that have values greater than 5 in each slice. How can I use logical index to do this, with no loops please? I would like to end up with an array...

## How can a loop involving recursive multiplication and accumulation be vectorized?

matlab,parallel-processing,vectorization
In matlab, I have a loop of the form: a=1; for (i = 1:N) a = a * b(i) + c(i); end Can this loop be vectorized, or partially unrolled?...

## how to retain monotonically increasing values of a one dimensional array

arrays,matlab,vectorization
Lets say i have a matrix, x=[1,2,3,5,4,6,6,7,4,2,3,4,5,6,10,12,4,5,43,23,45,34,54,78]; Now, I want to create a new matrix where i want to know the indices and values, of elements whose value is higher than (not equal to but strictly larger than) all the previous ones. so, the array i am looking for is,...

## Multiple constant to a matrix and convert them into block diagonal matrix in matlab

matlab,matrix,vectorization,diagonal
I have a1 a2 a3. They are constants. I have a matrix A. What I want to do is to get a1*A, a2*A, a3*A three matrices. Then I want transfer them into a diagonal block matrix. For three constants case, this is easy. I can let b1 = a1*A, b2=a2*A,...

## R apply function to data based on index column value

r,data.table,vectorization
Example: require(data.table) example = matrix(c(rnorm(15, 5, 1), rep(1:3, each=5)), ncol = 2, nrow = 15) example = data.table(example) setnames(example, old=c("V1","V2"), new=c("target", "index")) example threshold = 100 accumulating_cost = function(x,y) { x-cumsum(y) } whats_left = accumulating_cost(threshold, example\$target) whats_left I want whats_left to consist of the difference between threshold and the cumulative...

## Float comparisons failing without any obvious reason (32-bit X86 on Linux)

c++,linux,floating-point,vectorization
I have stumbled upon an interesting case of comparing (==, !=) float types. I encountered this problem while porting my own software from windows to linux. It's a bit of a bummer. The relevant code is the following: template<class T> class PCMVector2 { public: T x, y; public: bool operator...

## matlab: a tricky vectorization with a function enclosed with 2 nested loops

matlab,matrix,vectorization
I am going through a really hard time vectorizing the below code, unfortunately you need to look over it to understand what is going on. In gist, this function compares a set of data rows and creates a matrix of comparison relations. The two nested loops calls the inner function...

## Vectorized or single line evaluation of function array in MATLAB

matlab,vectorization,cell-array
In a previous question, a user asked about iterating over a cell array of anonymous functions. I am wondering whether there is a way to evaluate a set of functions without the explicit use of a for loop. As an example, the following code creates an array of (simple) functions,...

## how to solve many overdetermined systems of linear equations using vectorized codes?

multidimensional-array,scipy,vectorization,linear-algebra,least-squares
I need to solve a system of linear equations Lx=b, where x is always a vector (3x1 array), L is an Nx3 array, and b is an Nx1 vector. N usually ranges from 4 to something like 10. I have no problems solving this using scipy.linalg.lstsq(L,b) However, I need to...

## Parallel for loop in R

r,parallel-processing,vectorization
I have data.frame sent with sentences in sent\$words and dictionary with pos/neg words in wordsDF data frame (wordsDF[x,1]). Positive words = 1 and negative = -1 (wordsDF[x,2]). The words in that wordsDF data frame are sorted in decreasing order according to their length (length of string). I used this purpose...

## Can I do (x_i-x_j)^T(x_i-x_j) for x_i, x_j are rows in a X matrix with numpy native function instead of loop

python,performance,numpy,matrix,vectorization
I need to compute in numpy where \$x_i\$ and \$x_j\$ are rows in a matrix \$X\$. Now I am using loop, which is very slow. Is there any numpy native function allows such computation, like einsum: n=X.shape[0] Y=np.zeros((n,n)) for i in range(n): x=(X-X[i])**2 x=np.sum(x, axis=1) Y[i]=x return Y BTW, I...

## OpenMP SIMD on Power8

openmp,vectorization,simd,powerpc
I'm wondering whether there is any compiler (gcc, xlc, etc.) on Power8 that supports OpenMP SIMD constructs on Power8? I tried with XL (13.1) but I couldn't compile successfully. Probably it doesn't support simd construct yet. I could compile with gcc 4.9.1 (with these flags -fopenmp -fopenmp-simd and -O1). I...

## Assignment element of matrix without for-loop

matlab,for-loop,matrix,vectorization,linear-programming
I'm trying to build a constraint matrix that I'm going to use with linprog, and I'm struggling to build it efficiently without using for loops. Here is an example of what I'm trying to achieve: A = zeros(3, 3); % Constraint matrix i = [1 3]; % row indexes j...

## doing algebra with an MxNx3 array using vectorization in python?

arrays,vector,scipy,vectorization,linear-algebra
Suppose I have an MxNx3 array A, where the first two indexes refer to the coordinates a point, and the last index (the number '3') refers to the three components of a vector. e.g. A[4,7,:] = [1,2,3] means that the vector at point (7,4) is (1,2,3). Now I need to...

## change the format of a numpy array with no loops

python,numpy,matrix,vectorization
I have a numpy array with shape a.shape = (1,k*d)and i want to transform it to a numpy array with shape b.shape = (k*d,k) in each column b[i,j] = a[i] if j<i+1 b[i,j] = 0 if not for example: k = 3 d= 2 ********** A = |a| => B...

## 0xFFFF flags in SSE

c,vectorization,sse
I would like to create an SSE register with values that I can store in an array of integers, from another SSE register which contains flags 0xFFFF and zeros. For example: __m128i regComp = _mm_cmpgt_epi16(regA, regB); For the sake of argument, lets assume that regComp was loaded with { 0,...

## MATLAB vectorization: computing a neighborhood matrix

performance,matlab,matrix,vector,vectorization
Given two vectors X and Y of length n, representing points on the plane, and a neighborhood radius rad, is there a vectorized way to compute the neighborhood matrix of the points? In other words, can the following (painfully slow for large n) loop be vectorized: neighborhood_mat = zeros(n, n);...

## Matlab reshape back into original image

matlab,matrix,multidimensional-array,vectorization,reshape
I'm trying to reshape a multidimensional array into the original image. I have split an image of 512x512 pixels in sub-matrices of 8x8 pixels using the great solution that I found in this question: sub_images = permute(reshape(permute(reshape(i_image, size(i_image, 1), n, []), [2 1 3]), n, m, []), [2 1 3]);...

## Assigning a Matlab vector according to a function

matlab,vectorization
I basically want to vectorize the following: vect_y = zeros(1,numel(vect_x); for i = 1:numel(vect_x) vect_y = sum(vect_x(1:i)); end Is this possible? As an example, I was trying to use arrayfun the following way: y = arrayfun(@(y) sum(y), vect_x(1:1), vect_x(1:2), ..., vect_x(1:n)); But this won't work and it's not clean. edit:...

## Is there a reason to prefer '&&' over '&' in 'if' statements, other than short-circuiting?

r,vectorization,conventions
Yes I know, there have been a number of questions (see this one, for example) regarding the usage of & vs. && in R, but I have not found one that specifically answers my question. As I understand the differences, & does element-wise, vectorised comparison, much like the other arithmetic...

## How to vectorize the following python code?

python,arrays,performance,numpy,vectorization
I am trying to use Numpy and vectorization operations to make a section of code run faster but I don't succeed to find a solution. If somebody has an idea... Thanks. Here's the working code with loops: y = np.zeros(len(tab)) for i in range(len(tab)): s = 0 for n in...

## Vectorize loop to increase efficiency

matlab,vectorization
I have a 3 for loops and I would like if possible to vectorize the two inner loops. for t=1:size(datesdaily1) for i=1:size(secids,1) sum=0; if inc(t,i)==1 for j=1:size(secids,1) if inc(t,j)==1 sum=sum+weig1(t,j)*sqrt(Rates(t,j))*rhoneutral(i,j); end end b(t,i)=sqrt(Rates(t,i))*sum/MRates(t,1); end end end Any idea on how to accomplish that? Here 'weig', 'inc' and 'Rates' are (size(datesdaily1)...

## matlab : vectorize 4D matrix sum

performance,matlab,matrix,vectorization,nested-loops
I need to perform in MATLAB the following calculation: where w and v are vectors with N elements and A is a four-dimensional matrix (N^4 elements). This can be achieved by the following pedantic code: N=10; A=rand(N,N,N,N); v=rand(N,1); w=zeros(N,1); for pp=1:N for ll=1:N for mm=1:N for nn=1:N w(pp)=w(pp)+A(pp,ll,mm,nn)*v(ll)*v(mm)*conj(v(nn)); end end...

## Vectorize thinking

r,vector,vectorization
I'm having a vectorization problem. Say I'm having a vector x <- c(0,0,1,0,1,1,0) , what I want is to get the vector (1,0,1,1), or to remove all the 0s from the first spot to the first non-zero spot and all the 0s from last non-zero spot to the last spot....

## Relocate zeros to the end of the last dimension in multidimensional numpy array

python,arrays,python-2.7,numpy,vectorization
Say you have the following array of shape (5, 4, 3): x = array([[[ 0, 2, 1], [ 3, 4, 0], [ 8, 0, 6], [ 9, 0, 0]], [[12, 0, -14], [ 0, 16, 17], [ 0, -19, 20], [21, 22, 0]], [[24, 0, 26], [27, 0, 29], [...

## Numpy: Multidimensional index. Row by row with no loop

python,numpy,multidimensional-array,indexing,vectorization
I have a Nx2x2x2 array called A. I also have a Nx2 array called B, which tells me the position of the last two dimensions of A in which I am interested. I am currently getting a Nx2 array, either by using a loop (as in C in the code...

## Vectorization using accumarray

matlab,vectorization,accumulate,accumarray
I want to project the texture of 3D surface (CylCoors 300000x3) into a 2D plane (Image 380x360). For doing so I take every unique value in Z (UniqueZ=unique(CylCoors(:,3))) and and Theta (UniqueTheta=unique(CylCoors(:,1))) and project all the texture values (PointValues 300000x1) where both meet like this: Image=zeros(max(UniqueH),max(UniqueTheta)); %380x360 tic HMat=bsxfun(@eq,CylCoors(:,3),UniqueH'); %...

## Numpy vectorize and atomic vectors

python,numpy,vectorization
I would like to implement a function that works like the numpy.sum function on arrays as on expects, e.g. np.sum([2,3],1) = [3,4] and np.sum([1,2],[3,4]) = [4,6]. Yet a trivial test implementation already behaves somehow awkward: import numpy as np def triv(a, b): return a, b triv_vec = np.vectorize(fun, otypes =...

## How to build nested array of 1x3 vectors in the n,m posision of 2D matrix in MATLAb?

arrays,matlab,matrix,multidimensional-array,vectorization
How to build nested array of 1x3 vectors in the (n,m) posision of 2D matrix in a for loop? And then how to access the n,m vector ? Is there a better way than below. for n =1:2 for m =1:3 v = [n,m,n]' % test vector to be stored...

## Set specific rows of matrices in cell array to zero without using a for-loop

arrays,matlab,matrix,vectorization,cell-array
I'd like to replace a specific number of elements of my cell to zero without using for. For example to replace elements of row 2 in example cell a below: How should I proceed possibly using cellfun? a=cell(2,3); cellfun(@(x)(zeros(a{x}(2,:))),a); It gives the error "Bad cell reference operation". what if I'd...

## Vector-defined cross product application matrix and vectorization in Matlab

matlab,matrix,vector,vectorization,cross-product
I ran into an operation I cannot seem to achieve via vectorization. Let's say I want to find the matrix of the application defined by h: X -> cross(V,X) where V is a predetermined vector (both X and V are 3-by-1 vectors). In Matlab, I would do something like M=...

## Conditional maths operation on 2D numpy array checking on one dimension and doing different operations on diff dimensions

python,arrays,numpy,multidimensional-array,vectorization
I have a 2D numpy array where column 0 is the pan rotation of a device and column 1 is the tilt rotation. Each row is a different fixture. I want to run the following logic on each row: if(pantilt[0] > 90): pantilt[0] -=180 pantilt[1] *= -1 elif pantilt[0] <...

## Fastest way to compute upper-triangular matrix of geometric series (Python)

python,numpy,vectorization,linear-algebra
and thanks in advance for the help. Using Python (mostly numpy), I am trying to compute an upper-triangular matrix where each row "j" is the first j-terms of a geometric series, all rows using the same parameter. For example, if my parameter is B (where abs(B)=<1, i.e. B in [-1,1]),...

## Extracting values from indices without using loops

python,numpy,matrix,vectorization
So I have one matrix containing the data and a vector containing information on which data I should extract from the matrix. The real matrix is much longer but below is a short version just to illustrate what I meant. Data matrix = array([[1 2 3],[0 3 5],[1 4 4]])...

## Sum of outer products multiplied by a scalar in MATLAB

matlab,vectorization,product,outer-join
I would like to vectorize the sum of products hereafter in order to speed up my Matlab code. Would it be possible? for i=1:N A=A+hazard(i)*Z(i,:)'*Z(i,:); end where hazard is a vector (N x 1) and Z is a matrix (N x p). Thanks!...

## Can someone help vectorise this matlab loop?

matlab,vectorization
i am trying to learn how to vectorise matlab loops, so im just doing a few small examples. here is the standard loop i am trying to vectorise: function output = moving_avg(input, N) output = []; for n = N:length(input) % iterate over y vector summation = 0; for ii...

## Octave: how can these FOR loops be vectorized?

matlab,octave,vectorization
I am writing an Octave script to calculate the price of an European option. The first part uses Monte Carlo to simulate the underlying asset price over n number of time periods. This is repeated nIter number of times. Octave makes it very easy to setup initial matrices. But I...

## R - Vectorized implementation of ternary function

r,vectorization
I have three vectors X, Y and Z of equal length n. I need to create an n x n x n array of a function f(X[i],Y[j],Z[k]). The straightforward way to do this is to sequentially loop through each element of each of the 3 vectors. However, the time required...

## replace zero values with previous non-zero values

matlab,indexing,vectorization
I need a fast way in Matlab to do something like this (I am dealing with huge vectors, so a normal loop takes forever!): from a vector like [0 0 2 3 0 0 0 5 0 0 7 0] I need to get this: [NaN NaN 2 3 3...

## Vectorize For-If-Elseif Loop

matlab,for-loop,vector,vectorization,parfor
I am struggling with vectorizing this parfor loop. I want to completely remove the parfor loop from the code as it is taking a long time to execute when n is large. Please see the code pasted below. I will appreciate any tips/advice/help any person in this forum can give...

## Speed-efficient classification for complex vectors in MATLAB

matlab,optimization,vectorization,nested-loops,pdist
I am trying to optimize this piece of code and get rid of the nested loop implemented. I am finding difficulties in applying a matrix to pdist function For example, 1+j // -1+j // -1+j // -1-j are the initial points and i am trying to detect 0.5+0.7j to with...

## Vectorize MATLAB for loop

matlab,vectorization
I have the following lines of code y = zeros(n, 1); for i=1:n b = L * [u(i:-1:max(1,i-M+1));zeros((-i+M)*(i-M<0),1)]; y(i) = b' * gamma; end u is nx1, gamma is Mx1 and L is MxM n takes very large values, so are there any ideas on how to vectorize the for...

## Image Vectorizer

image-processing,vectorization,object-recognition
I'm looking for a library/tool/image processing technique which can create vectors out of images (similar to text vectorization like TFIDF or so). Can anybody share some ideas how to proceed?

## How to initiate for loop to run for a number of iteration in MATLAB

matlab,for-loop,nested,vectorization
sorry again for multiple silly questions in a day. I am very novice and do not know programming well. I have asked a simple question about for loop here, which has been sorted out: How to divide two different array in for loop matlab I need to initiate this for...

## Vectorize MATLAB loop

performance,matlab,image-processing,vectorization
I have the following MATLAB code : meanv = [rmean,gmean,bmean]; for i = 1:1:image_info.Height for j = 1:1:image_info.Width % for every pixel x = image_rgb(i,j,1:3); pix_color = [x(:,:,1),x(:,:,2),x(:,:,3)]; d = dist(meanv,pix_color'); if d <= threshold S(i,j) = 1; end end end The above fragment is part of a program that,...

## Rvest loop breaks on redirecting site

r,for-loop,web-scraping,vectorization,rvest
My situation: I have a long (20k lines) list of URLs that I need to scrape particular data elements from for an analysis. For the purpose of this example, I'm looking for a particular field called "sol-num", which the the solicitation number. Using the following function, I can fetch the...

## MATLAB - Evaluate a function at each point in meshgrid?

matlab,vectorization
I wish to plot the heat map of a bivariate (independent) Gaussian. To plot it over a 2D square, I did joint_pdf = @(m, s) normpdf(m, 1, 1)*normpdf(s, 1, 1); [x, y] = meshgrid(0:0.1:10, 0:0.1:10); prob_map = zeros(numel(x), numel(y)); for idx1 = 1:size(prob_map, 1) for idx2 = 1:size(prob_map, 2) prob_map(idx1,...

## Numpy: argmax over multiple axes without loop

python,numpy,vectorization,argmax
I have a N-dimensional array (Named A). For each row of the first axis of A, I want to obtain the coordinates of the maximum value along the other axes of A. Then I would return a 2-dimensional array with the coordinates of the maximum value for each row of...

## Vectorize np.arange or equivalent

python,numpy,vectorization
I have a long 1D array. I'd like to create an array that is the result of np.arange() applied to each value in the array plus some constant. E.g if the constant = 3 and my array looks like [1,2,3,4,5] I'd like to get [[1,2,3] [2,3,4] [3,4,5] [4,5,6] [5,6,7]] np.arange()...

## Returned dtype of numpy vectorized function

python,numpy,vectorization
I have an issue regarding the dtype of the returned numpy array of a vectorized function. My function returns a number, eventually a fraction. Strangely the position of the fraction seems to influence the returned dtype. I want the type always to be object if the function returns a fraction....

## Vectorizing a Numpy slice operation

python,numpy,vectorization
Say I have a Numpy vector, A = zeros(100) and I divide it into subvectors by a list of breakpoints which index into A, for instance, breaks = linspace(0, 100, 11, dtype=int) So the i-th subvector would be lie between the indices breaks[i] (inclusive) and breaks[i+1] (exclusive). The breaks are...

## Unique elements in each column of an array (Matlab)

arrays,matlab,unique,element,vectorization
I might be a bit rusty with Matlab, maybe the answer to this question is more trivial than I imagine at this moment. However I have searched online for efficient solutions and I haven't found any, so I will try here. I have a large matrix, something like y here:...

## Efficient way to perform running total in the last 365 day window

r,vectorization,dplyr,zoo,rollapply
This is what my data frame looks like: 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...

## Making this C array processing code more python (and even numpy)

python,c,arrays,numpy,vectorization
I'm trying to get my head around the amazing list processing abilities of python (And eventually numpy). I'm converting some C code I wrote to python. I have a text datafile where first row is a header, and then every odd row is my input data and every even row...

## Using arrayfun to apply two arguments of a function on every combination

octave,vectorization
Let i = [1 2] and j = [3 5]. Now in octave: arrayfun(@(x,y) x+y,i,j) we get [4 7]. But I want to apply the function on the combinations of i vs. j to get [i(1)+j(1) i(1)+j(2) i(2)+j(1) i(2)+j(2)]=[4 6 5 7]. How do I accomplish this? I know I...

## Fastest Way to Generate x Number of Random Variables

matlab,vectorization,computation-theory
index = [10 100 1000 10000 100000] Let's say I wanted to generate 10, 100,...,100000 (corresponding to index above) exponential random variables with parameter 1/10. I of course did this with a for loop and with the command for example x = exprnd(0.1,100,1) One can also generate the exponential random...

## Efficient element-wise function computation in Python

python,numpy,scikit-learn,vectorization
I have the following optimization problem. Given two np.arrays X,Y and a function K I would like to compute as fast as possible the matrix incidence gram_matrix where the (i,j)-th element is computed as K(X[i],Y[j]). Here there an implementation using nested for-loops, which are acknowledged to be the slowest to...

## Cumsum and vectorized slicing

python,numpy,vectorization
I have a matrix J of size (j,v) and a vector JTildeIDX of size v. The vector contains the start for a cumsum operation over J. That is, given >>> JTildeIDX array([0, 0, 9, 9, 9]) >>> J array([[ 1. , 1. , 1. , 1. , 1. ], [...

## How to append a vector to a vector r - in a vectorized style

r,vector,vectorization
We all know that appending a vector to a vector within a for loop in R is a bad thing because it costs time. A solution would be to do it in a vectorized style. Here is a nice example by Joshua Ulrich. It is important to first create a...

## How to generate a matrix of vector combinations with zeros for excluded elements?

matlab,matrix,vector,combinations,vectorization
I want to create a matrix from all combinations of elements of one vector that fulfill a condition For example, I have this vector a = [1 2 3 4 5] and want to create a matrix like a = [1 0 0 0 0; 1 2 0 0 0;...

## Fastest R equivalent to MATLAB's reshape() method?

r,performance,matlab,vectorization,reshape
I am converting a MATLAB script into R and regretting it so far, as it is slower at the moment. I'm trying to use "vectorized functions" as much as possible, but I'm relatively new to R and do not know what is meant by this. From my research for loops...

## Vectorising a for loop containing a which statement and a function

r,vectorization
A reproducible example of the code I'm trying vectorise. cutOffs <- seq(1,10,0.2) plotOutput <- matrix(nrow=length(cutOffs), ncol=2) colnames(plotOutput) <- c("x","y") plotOutput[,"y"] <- cutOffs for(plotPoint in 1:length(cutOffs)) { plotOutput[plotPoint, "x"] <- nrow(iris[ which(iris\$Sepal.Length > cutOffs[plotPoint] & iris\$Sepal.Width > cutOffs[plotPoint]), ]) } plotOutput Specifically what I'm looking to find out is, if there's...

## Python : How to vectorize my split function with Numpy

python,numpy,vectorization
I found on Stack a function who solve my problem but right now I would like to speed up my code because I have a lot of list to split. I heard that vectorize a function ca be a solution so tried to vectorize my function with numpy but it...

## vectorized simulation in R

r,simulation,vectorization,lapply
I've written a simulation function in R. I'd like to do num simulations. Rather than using a for loop, I'm trying to use some sort of apply function, such as lapply or parallel::mclapply. lapply, as I'm currently using it, is failing. For example: # t1() is a generic example function...

## Multiplying every element of one array by every element of another array

python,arrays,numpy,vectorization,cartesian-product
Say I have two arrays, import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([5, 6, 7, 8]) What's the fastest, most Pythonic, etc., etc. way to get a new array, z, with a number of elements equal to x.size * y.size, in which the elements are...

## How to pick only efficient frontier points in a plot of portfolio performance?

r,for-loop,vectorization,portfolio
The name of this question does not do it justice. This is best explained by numerical example. Let's say I have the following portfolio data, called data. > data Stdev AvgReturn 1 1.92 0.35 2 1.53 0.34 3 1.39 0.31 4 1.74 0.31 5 1.16 0.30 6 1.27 0.29 7...

## Take value from nth column of a data frame, for n different for each row

r,data.frame,vectorization
How do I construct a vector of values from nth column of some data frame, where n is a per-row value defined in some vector? Example: > df <- data.frame(a=c(100, 110, 120, 130, 140), b=c(200, 210, 220, 230, 240), c=c(300, 310, 320, 330, 340)) > df a b c 1...

## Numpy sum running length of non-zero values

python,arrays,performance,numpy,vectorization
Looking for a fast vectorized function that returns the rolling number of consecutive non-zero values. The count should start over at 0 whenever encountering a zero. The result should have the same shape as the input array. Given an array like this: x = np.array([2.3, 1.2, 4.1 , 0.0, 0.0,...

## when is vectorization favored in Julia?

vectorization,julia-lang
I have 2 functions for determining pi numerically in Julia. The second function (which I think is vectorized) is slower than the first. Why is vectorization slower? Are there rules when to vectorize and when not to? function determine_pi(n) area = zeros(Float64, n); sum = 0; for i=1:n if ((rand()^2+rand()^2)...

## Combining vectorization and recursion in R?

r,recursion,vectorization
I am trying to combine vectorization and recursion in this implementation of the factorial function: fac <- function(n) { ifelse(n == 1, 1, n * fac(n-1)) } fac(6) #720 fac(c(6,7)) #Error: evaluation nested too deeply: infinite recursion / options(expressions=)? #Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?...

## Create a horizontically stretched upper triangular matrix

matlab,matrix,vectorization,memory-efficient
I'd like to create a 4x12 matrix which is very similar to a upper triangle matrix, it looks like this: 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0...

## Sequence vectorized

matlab,sequence,vectorization
How is it possible to create in a vectorized way, in Matlab, a sequence if I've the 2 vectors of startings and ending of the subsequences ? Example Input: A=[12 20 34] B=[18 25 37] I want to get C=[12 13 14 15 16 17 18 20 21 22 23...

## comparison of loop and vectorization in matlab

matlab,loops,vectorization
let us consider following code for impulse function function y=impulse_function(n); y=0; if n==0 y=1; end end this code >> n=-2:2; >> i=1:length(n); >> f(i)=impulse_function(n(i)); >> returns result f f = 0 0 0 0 0 while this code >> n=-2:2; >> for i=1:length(n); f(i)=impulse_function(n(i)); end >> f f = 0...

## How (in a vectorized manner) to retrieve single value quantities from dataframe cells containing numeric arrays?

r,dataframes,vectorization
I've got a dataframe that includes columns like the one on the right here: lengthArray speed_max 1 4 24, 18, 24, 18 2 10 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 3 4 -999, -999, -999, -999 4 2 -999, -999 5 2 18, 18 6 1...

## Array of Structures (AoS) vs Structure of Arrays (SoA) on random reads for vectorization

c++,parallel-processing,vectorization,cpu-cache
My question is in regard of the following phrase from the book: Unfortunately, the SoA form is not ideal in all circumstances. For random or incoherent circumstances, gathers are used to access the data and the SoA form can result in extra unneeded data being read into cache, thus reducing...

## Signal segmentation with overlaps

performance,matlab,matrix,vectorization
I have some signals. They are all the same length (N = 1024). I have to break each into Ns = 7 segments with L = 256 points so there will be 50% overlapping. S = randi(10,[4 N]); can be thought as 4 signals. I was thinking about saving the...

## How to speed up or vectorize a for loop?

r,performance,for-loop,vectorization,rcpp
I would like to increase the speed of my for loop via vectorization or using Data.table or something else. I have to run the code on 1,000,000 rows and my code is really slow. The code is fairly self-explanatory. I have included an explanation below just in case. I have...

## vectorization for array products

matlab,vectorization
How can I vectorize this loop ? Let b = real array of size (2,3) M = real array of size (7,2) y = real array of size (n,3) b and M are constant and n is "big" (in my problem, n > 80000). I want to optimize (vectorize) this...

## combining repmat and transpose in julia

vectorization,julia-lang
I'm porting some code from R to julia to get familiar with the language, and I found a few patterns that don't translate smoothly. Consider the following function, # Ricatti-Bessel and derivatives up to nmax, vectorised over x function rb(x, nmax) n = 1:nmax nu = 0.5 + [0, n]...

## Subtract a constant value from elements in a non-scalar struct array in a vectorized way

performance,matlab,vectorization
I have a struct like this temp_struct(1).budget=8 temp_struct(2).budget=8 and I want to subtract a constant value from both of them (replacing 8 with the new value). How can i do it more efficiently without using a loop in matlab?...

## Improve Speed of Piecewise Function in MATLAB

matlab,optimization,vectorization
I have a small piecewise function that profiling reveals is taking 60% of the runtime of the program. It is called very often because it goes within some integrals that I perform quite a lot in my code. According to profiling, it is called 213560 times, taking 47.786 s in...

## using reshape for a mean in a 3D matrix

matlab,matrix,vectorization
I have a 3D (x,y,nframes) matrix/ movie ( named ch), and a logical mask (x,y). I want to do the mean of the mask pixels in each frame , at the end I get a vector of dim 1xnframes. And I want to do it with the reshape instead than...

## Vectorize this, filter string arrays by a string? GPU pipeline?

ios,arrays,swift,search,vectorization
I did search SO but didn't find for using something like MPICH or vectorization. In fortran, I'd use MPICH to break up the code to paralyze. This is to run on an iPad with Metal available. I have a 1-D string array with many hundred of thousands of elements, many...

## Matrix optimization in NLopt

vectorization,julia-lang,nlopt
NLopt is a solver for optimization, which implements different optimization algorithms and is implemented in different languages. In order to use the LD_LBFGS algorithm in Julia, does the variable have to be a vector as opposed to a matrix? If yes, once we need to optimize an objective which is...

## Faster convolution of probability density functions in Python

python,numpy,vectorization,convolution,probability-density
Suppose the convolution of a general number of discrete probability density functions needs to be calculated. For the example below there are four distributions which take on values 0,1,2 with the specified probabilities: import numpy as np pdfs = np.array([[0.6,0.3,0.1],[0.5,0.4,0.1],[0.3,0.7,0.0],[1.0,0.0,0.0]]) The convolution can be found like this: pdf = pdfs[0]...

## how to calculate the norm of a vector in a large mxnx3 array?

python,arrays,scipy,vectorization,linear-algebra
Suppose I have an array of the shape (m,n,3), where m and n refers to the y and x coordinates of a point, and the 3 numbers in each point refer to a three-dimensional vector. (A similar situation is an image with height m and width n, and 3 refers...

## create a matrix from array of elements under diagonal in numpy

python,arrays,numpy,matrix,vectorization
I would like to create a matrix using a list whose elements would be the elements of the matrix under the diagonal. import numpy as np x1 = np.array([0.9375, 0.75, 0.4375, 0.0, 0.9375, 0.75, 0.4375, 0.9375, 0.75, 0.9375]) x1 the matrix I would like to have is array([[ 1. ,...

## Set time series vectors lengths equal (resize/rescale them with use of linear interpolation)

r,datetime,vector,vectorization
I have huge dataset of time series which are represented as vectors (no time labels available), due to some errors in measuring process their lengths (as values from length() show) varies slightly (~10%) but each of them definitively describs time interval of exacly two minutes. I would like to rescale/resize...

## Efficiently checking Euclidean distance for a large number of objects in Python

python,numpy,vectorization,computational-geometry
In a route planning algorithm, I'm trying to perform a filter on a list of nodes based on distance to another node. I'm actually pulling the lists from a crude scene graph. I use the term "cell" to refer to a volume within a simple scenegraph from which we've fetched...

## Way to vectorize this loop? Multiply two matrices, store information, do this many times without looping

r,loops,matrix,vectorization,apply
Suppose (small numbers in this example) I have an array that is 3 x 14 x 5 call this set.seed(1) dfarray=array(rnorm(5*3*14,0,1),dim=c(3,14,5)) I have a matrix that corresponds to this and is 39 (which is 13*3) x 14 Call this matrix: dfmat = matrix(rnorm(13*3*14,0,1),39,14) dfmat = cbind(dfmat,rep(1:3,13)) dfmat = dfmat[order(dfmat [,15]),]...

## Select submatrix and vectorize in one command in MATLAB

arrays,matlab,vectorization,reshape,submatrix
Lets say we have an Array A = ones(2, 2, 2) and another matrix P = rand(4). I am wondering if it is possible to write the code temp = A(:, :, 1); X = P * temp(:); into one line of code to save the memory consumed by temp....