I am working on a project for which I need to compute a lot of inner products in high dimensions. I am aware that we should always try to vectorize operations in matlab, but I am not sure how to do this ...
Lets say we have two matrices, A
and B
of size N x d
where N
is the number of inner products to compute in d
dimensions.
It is easy to implement using for-loops, but I suspect more efficient ways exist. A for-loop implementation might look like this:
innerprods=zeros(N,1);
for i=1:N
innerprods(i)=A(i,:)*B(i,:)';
end
Does anyone have ideas how to vectorize this? I guess bsxfun
should come into play at some point, but I cannot figure out how to use it for this ...
Thanks in advance!