# Tag Info

## Hot answers tagged vectorization

### My blit function for my own graphics library

it seems to be optimized quite well by the compiler, as it uses a lot of vectorized instructions Well, kind of. GCC didn't use vectorization at all, and Clang used some of it but in a very strange ...
• 7,409
Accepted

### Scaling and incrementing non-zero elements of a NumPy matrix

This is a typical use case for numpy.where: cPrime = np.where(C, C * 30 + 1, C) This is about twice as fast as ...
• 49.4k
Accepted

### Vectorized and Multi Threaded Image Convolution

One problem that immediately jumps out at me is the loop-carried dependency through addps, which has a latency of either 3 or 4 (depending on the processor) while ...
• 7,409
Accepted

### Implementation of linear regression in Python

Just reviewing normalizeFeatures. Instead of a comment explaining what the function does, write a docstring. (Docstrings are available from the interactive ...
• 49.4k

### AVX Vectorized Multi-threaded Mandelbrot Renderer

There are some & 0xff operations that are not necessary: (aMask & (~iMask & 0xff)), because the bits reset by ...
• 7,409
Accepted

### Rolling regressions in R

For each group in your data table, your code computes the coefficient b1 from a linear regression y = b0 + b1*x + epsilon, and ...
• 1,136
Accepted

### Improving Matlab function within big simulation

Vectorization If you look at all the calls to mean, you see that what you are calculating in each loop is a "Cumulative moving average", but in the oposite ...
• 2,057

### Biometric matching score calculator

Regarding the vectorized code: permute can be considered a "Zero-cost operation". Logical operations are performed fastest using ...
• 2,057

### My blit function for my own graphics library

There are a few things that can be lifted out of loops. The three screen related variables, screen_height, screen_width, and <...
• 7,646

### Improving Matlab function within big simulation

A few notes: I'm confused about the design of your function. If you are only processing column vectors, why not just process a row vector instead? That's what I would expect when calling this ...
• 21.8k
Accepted

### Generate random unit vectors around circle

To answer your question, you need to add a new dimension to the ndarray: vecs /= mags[..., np.newaxis] However uniformly ...
• 9,563
Accepted

### Optimization of multiple arrays for filtering

There may exist a way to make this particular loop run faster. In fact, I'm sure there probably is... however, it's kind of a futile effort. There's a lot of time also being wasted elsewhere where ...
• 25.2k
Accepted

### Remove duplicates in list of lists where an item in each list is ignored when determining duplicity

Maybe there's an elegant way for me to instead not delete the e column and still remove duplicates efficiently without considering the e values? Your set-based ...
• 2,738
Accepted

### Compute a numerical derivative

You can use np.roll to compute the centered differences as a vectorised operation rather than in a for loop: ...
Accepted

### Matlab Computer Vision

There's a lot going on in this script, but I've tried to cover all of the important parts. Style and conventions: I think your coding style is very good. You're using nice a descriptive variable ...
• 2,057
Accepted

### Evaluating a series of Legendre polynomials

Compute legendre(n) once per loop iteration instead of twice. Use np.hypot instead of ...
• 49.4k

### Generate random unit vectors around circle

If you just want to do this for the unit circle, you can use numpy.random.uniform to choose the angle $\theta$ defining the unit vector. If you want to deal with ...
• 16.2k

### Generic pixel class to seamlessly alpha-blend and convert between different pixel structure layouts

The code seems to get more and more questionable as we read downward. Starting at the bottom: ...
• 19.2k

### Efficient element-wise function computation in Python

You can avoid the nested loops using numpy.meshgrid to build a table of entries in x and y, ...
• 49.4k
Accepted

### Vectorized numpy version of arange with multiple start stop

This code looks good to me: the docstring is clear and the implementation is simple and efficient. So I have only a few minor points. The code doesn't fit in 80 columns, meaning that we have to ...
• 49.4k

### Extract the largest value for each day from a matrix

A few notes: (I implemented this in Octave, so there may be differences between my code and a proper Matlab implementation, but there shouldn't be). First we care about only unique dates, right? So ...
• 21.8k
Accepted

### Horizontally concatenating the entries of two matrices, and padding with NaNs

I see two major way to simplify and speed this up. First, rather than copying a to c at every step of the loop, you can define <...
• 2,364
Accepted

### Converting Array of Floats to UINT8 (char) or UINT16 (unsigned short) Using SSE4

why not load 4 packed __m128 and then store 32 Pixels at once I think that's 16 pixels, but it's a good plan. The pack instructions were used inefficiently (in the linked question that was not really ...
• 7,409

### Compute a numerical derivative

You can vectorize the calculation ...
• 9,020
Accepted

### Find the minimum value that data could have had before it was rounded

You can firstly change by difference != 0 and then use na.locf to replace NAs by last ...
• 366
Accepted

### Determining whether a list of pathways and its genes are all included in another list

If you were to add a cat("hello") at the top of your all_in function, you would find that your function is called 25 times, once ...
• 3,495
Accepted

### Vectorizing matrix operation instead of for loop in circular matrices

This is easy using numpy.roll, for example: zx = np.roll(x, 1) * (np.roll(x, 2) + np.roll(x, -1)) - x
• 49.4k
Accepted

### PyTorch Vectorized Implementation for Thresholding and Computing Jaccard Index

I think a different approach is needed to achieve a better performance. The current approach recomputes the Jaccard similarity from scratch for each possible threshold value. However, going from one ...
• 2,301
Accepted

### Vectorizing equations for fsolve

Yes. You can simplify this code. I made two main changes. First, I forget that $x_{50}$ is known and write an equation for it just like all the other variables, then I replace that equation by the ...
• 241