When comparing two vectors it is simple to calculate the angle between them, but in R it is noticeably harder to calculate the angle between a vector and a matrix of vectors efficiently.
Say you have a 2D vector A=(2, 0) and then a matrix B={(1,3), (-2,4), (-3,-3), (1,-4)}. I am interested in working out the smallest angle between A and the vectors in B. If I try to use
min(acos( sum(a%*%b) / ( sqrt(sum(a %*% a)) * sqrt(sum(b %*% b)) ) ))
it fails as they are non-conformable arguments.
Is there any code similar to that of above which can handle a vector and matrix?
Note: At the risk of being marked as a duplicate the solutions found in several sources do not apply in this case
Edit: The reason for this is I have a large matrix X
, and A
is just one row of this. I am reducing the number of elements based solely on the angle of each vector. The first element of B
is the first in X
, and then if the angle between any element in B
and the next element X[,2]
(here A
) is greater than a certain tolerance, this is added to the list B
. I am just using B<-rbind(B,X[,2])
to do this, so this results in B
being a matrix.
B
meant to be a list or a matrix? A list I suppose.B=list(c(1,3), c(-2,4), c(-3,-3), c(1,-4))
?B
is created which answers this