Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiply two vectors - I want a scalar but I get a vector?

Tags:

r

vector

This is my code:

a <-c(1,2,3)
b <-t(a)
print(a*b)

I would expect the result to be 14, since a column vector multiplied with a row vector with fitting dimensions should be a skalar.

However, I get:

print (a*t(a))

 [,1] [,2] [,3]

[1,]    1    4    9

Hence the partial sums instead of the whole sum. How can I fix this?

like image 207
user1862770 Avatar asked Dec 08 '12 16:12

user1862770


1 Answers

If what you essentially want is the sum of the products, then all you need is sum(a*a)

like image 63
A_K Avatar answered Sep 18 '22 18:09

A_K