Bsxfun in Matlab using multiple operations with Matrices and Vectors

349 Views Asked by At

I'm trying to vectorize a few matlab operations using bsxfun. Looking at bsxfun documentation it looks like I can only do two operations at a time. But the operation I'm trying to do has a few operations.

# delta_g is 100 X 1
# z is 100 X 1
# W is 100 X 10
# delta 10 X 1

for j = 1:100
    delta_g(j) = (1 - z(j)) * (W(j,:) * delta);
end

Below is the code that I am trying to use.

temp = bsxfun(@times, W, delta.');
temp1 = 1 - z;

My rationale is to break it up into two operations and then multiple temp and temp1 together but the dimensions don't work out for my solutions? What would be the best way to vectorize this and is it possible be able to do all of it in one bsxfun operation? Any help is greatly appreciated!

1

There are 1 best solutions below

0
On BEST ANSWER

In general, you would need to use bsxfun several times, because each bsxfun can only carry out one operation.

But in this case it can be done just with matrix multiplication and element-wise multiplication:

delta_g = ((1-z).*(W*delta)).';