1
votes

Are there some nice way to do following.

I have 2 vectors where I want to only make sub vector multiplications. For examples,

a = 1:6;  b = (1:6)'

Then I'd like the result:

result = [1*1+2*2+3*3; 4*4+5*5+6*6] = [14; 77]

So, I'd like to multiply each sub vectors of 3 element with each others. In the end, last element of the vector result would then be the sum or the result of a*b

Thank you in advance for your help

2

2 Answers

2
votes

This can be done as

sum(reshape(a,3,[]).*reshape(b,3,[])).'

or

dot(reshape(a,3,[]),reshape(b,3,[])).'
2
votes

maybe I'm missing something, but isn't it as simple as:

>> [a(1:3)*b(1:3) a(4:6)*b(4:6)]
ans =

   14   77

??