vectorization

Best way to vectorize C code by hand

Hi, I want to vectorize by hand some C code, in order to it speedup. For that purpose (SPE on the Cell processor or CBE) I want to use SIMD math. The code originally uses some physical vector calculations (speed, acceleration, etc), so in some parts of the code there is a lot of operations like; ax=a*vx+b*rx; ay=a*vy+b*ry; az=d*vz+b*rz...

MATLAB: inconsistent results using isreal

Take this simple example: a = [1 2i]; x = zeros(1,length(a)); for n=1:length(a) x(n) = isreal(a(n)); end In an attempt to vectorize the code, I tried: y = arrayfun(@isreal,a); But the results are not the same: x = 1 0 y = 0 0 What am I doing wrong? ...

Is there a vectorized way to operate on a different number of values per column in MATLAB?

In MATLAB, is there a more concise way to handle discrete conditional indexing by column than using a for loop? Here's my code: x=[1 2 3;4 5 6;7 8 9]; w=[5 3 2]; q=zeros(3,1); for i = 1:3 q(i)=mean(x(x(:,i)>w(i),i)); end q My goal is to take the mean of the top x% of a set of values for each column. The above code works, but I'm ...

How to vectorize a random walk simulation in Matlab

I am rewriting a monte carlo simulation model in Matlab with an emphasis on readability. The model involves many particles (represented as (x,y,z)) following a random walk over a small set of states with certain termination probabilities. The information relevant for output is the number of particles that terminate in a given state. Th...

MATLAB vectorisation problem

Let x=1:100 and N=1:10 I would like to create matrix x^N so that the ith column contains the entries [1 i i^2 ... i^N] I can easily do this using for loops. But is there a way to do this using vectorized code? ...

Incrementing one value of a MATLAB array multiple times in one line

This is a question about incrementing one value of a MATLAB array multiple times in the same statement, without having to use a for loop. I set my array as: >> A = [10 20 30]; And then run: >> A([1, 1]) = A([1, 1]) + [20 3] A = 13 20 30 Clearly the 20 is ignored. However, i would like it to be included, so that: >> A ...

Find values in a matrix and put them into a vector

It must be simple, but surprisingly I couldn't find an answer to this problem here or by trial-and-error. I want to get values out of a matrix (according to some condition) and place the values into a vector. I also need the subscript indices of the matching values. There is a lot of data so for loops are out. This is a correct (but...

MATLAB: how to assign values on the diagonal?

Suppose I have an NxN matrix A, an index vector V consisting of a subset of the numbers 1:N, and a value K, and I want to do this: for i = V A(i,i) = K end Is there a way to do this in one statement w/ vectorization? e.g. A(something) = K The statement A(V,V) = K will not work, it assigns off-diagonal elements, and this is no...