I have written below code to calculate a matrix:
vec0=repmat(vec,1,9);
triw = (bsxfun(@times,vecO(1,:)',yc1)-bsxfun(@times,vecO(2,:)',xc1)).*(bsxfun(@times,vecO(2,:)',yc1)+bsxfun(@times,vecO(1,:)',xc1));
vec is a 2-by-900 matrix and xc1 and yc1 are 8100-by-900. I use this code in a loop. It is very slow, so I'd like to improve its performance. How would I do this?
I was able to speed things up by around 30-40% by reorganizing the calculations into 2 calls to
bsxfuninstead of 4:Note that I also used the array transpose operator
.'instead of the complex conjugate transpose operator'. The first simply reorganizes the array without modifying the values, while the second can give you different results if you're dealing with complex data.Here's the code I used to compare the two approaches:
The maximum absolute difference between the two results was on the order of the floating-point relative accuracy for double-precision numbers, so effectively no difference.