I have a double summation which I am trying to compute as follows
$$\sum_i^3\sum_j^3 h_{i,j} w_{i,j}$$
which I am trying to implement in MATLAB.. My application gives me the explicit values of both $h_{i,j} $ and $w_{i,j}$ and gives them as vector such as
For example $${\bf h} = [ h_{11}\,\, h_{12}\,\, h_{13} \,\,h_{21}\,\, h_{22}\,\, h_{23} ]^T$$ $${\bf w} = [ w_{11}\,\, w_{12}\,\, w_{13} \,\,w_{21}\,\, w_{22}\,\, w_{23} ]^T$$
where $T$ means transpose. In order to find the double summation, I do the following in MATLAB
for l =1: 6 v(i)= h(i)*w(i) end
for i = 1:6
summation = sum_initial+ v(i)
endAm I doing things correctly here?
Thanks,
$\endgroup$ 73 Answers
$\begingroup$you defined your vectors as two rows, then
just $h.'*w$ or if $h$ is real then $h'*w$ enough, this is a row vector times column vector, i.e. a dot\inner product
however, if you want them to be matrices (as it looks from your mathematical definition), then $h(:).'*w(:)$ will do the trick.
Your code could work if you fix it as following
for l =1: 6 v(i)= h(i)*w(i);
end
summation=0;
for i = 1:6 summation = summation + v(i);
end $\endgroup$ 3 $\begingroup$ Maybe you are looking for something like the bsxfun function, which performas a binary operation, such as addition or multiplication, element-wise on two arrays:
summation = sum( bsxfun(@times, h, w) ) $\endgroup$ $\begingroup$ Assuming h and w are 3x3 matrices:
$\endgroup$ 2h(:).'*w(:)