Search code examples
rproject

Projection of 2 vectors in R


I want to calculate the projection u⟶v

Definition: proj(u⟶v)=u⋅v|v|2v

# my 2 vectors 
u<-c(1.5,sqrt(3)/2)
v<-c(2,0)

The answer should look as the following :

[1] 1.5 0.0

Solution

  • Not entirely sure what you are looking for.

    u <- c(1.5,sqrt(3)/2)
    v <- c(2,0)
    
    as.vector( (u %*% v) / (v %*% v) ) * v
    

    yields

    [1] 1.5 0.0
    

    You can wrap it into a function

    proj <- function(u, v) {
     return(as.vector( (u %*% v) / (v %*% v) ) * v)
    }