Finding dot product in r
Asked Answered
Z

4

13

I am trying to find the dot product of two matrices in R. In the q matrix, which must be transposed, I have three different q values that I randomly generated earlier, and in the z matrix three randomly generated z values that serve as coordinates of a random point i. I have:

    z0= NULL
    for (i in 1:100){
        z0[i]= 1
    }
    z1= runif(100, min=0, max= 20)
    z2= runif(100, min=0, max=20)
    q0= runif(1, 0, 1)
    q1= runif(1, 0, 1)
    q2= runif(1, 0, 1)
    i= runif(1, 1, 101)
    i= ceiling(i-1)
    q= matrix(c(q0,q1,q2), ncol=3)
    z= matrix(c(z0[i],z1[i],z2[i]), ncol=3)
    s[i]= t(q)*z

However, when I try to calculate s[i], I get Error in t(q) * z : non-conformable arrays. I am not sure why this would be as I they seem to both have the same length.

This is my first time using R so I am not really sure what is going on.

Thanks!

Zoie answered 3/2, 2015 at 8:17 Comment(17)
You have to show a reproducible example q= matrix(c(q0,q1,q2), ncol=3) Error in matrix(c(q0, q1, q2), ncol = 3) : object 'q0' not foundSadler
Dot product is %*%.Oculomotor
@akrun, I have made it reproducible, sorry for not doing that before.Leprosarium
@JonathanO'Farrell Have you tried Pascal's suggestionSadler
@Pascal, I changed it to %*% but not get the error: Warning message: In s[i] = t(q) %*% z : number of items to replace is not a multiple of replacement length.Leprosarium
@JonathanO'Farrell Based on the example, it worked.Sadler
@Sadler do you mean my example in the question changed to use %*%? With that I get the error that I mentioned in my last comment to PascalLeprosarium
@Khashaa I get the same error when I use crossprodLeprosarium
@JonathanO'Farrell It is better to set a seed for making it reproducible. i.e. set.seed(1) before the runif. This is what I get crossprod(q, z) [,1] [,2] [,3] [1,] 0.7622588 10.68985 14.67339 [2,] 0.8413184 11.79857 16.19527 [3,] 0.8964904 12.57230 17.25733Sadler
Where did you define s?Endsley
@Sadler are you using exactly what I wrote just with crossprod(q,z?). Are you using the R console? If so, we are doing the same things so I'm not sure why it is not working for me...Leprosarium
@Endsley I should have included s= NULL , sorryLeprosarium
@JonathanO'Farrell Set some seed. set.seed(1); z1= runif(100, min=0, max= 20);....crossprod(q,z)Sadler
It worked!! Thanks. But now how do I get the dot product from this?Leprosarium
@Sadler forgot to tag youLeprosarium
@Endsley I got the same error "rror in v %*% w : non-conformable arguments". crossprod(v, w) helped. Thanks.Salter
a,b are both matrix: then a %*% bRoving
M
15

Without using matrices or any special libraries:

The dot product of two vectors can be calulated by multiplying them element-wise with * then summing the result.

a <- c(1,2,3)
b <- c(4,5,6)

sum(a*b)
Montiel answered 11/11, 2020 at 0:43 Comment(1)
A special case that only works for vectors: The OP asked for matrices.Inquiline
C
7

As Pascal says, dot product in R is %*%. I am able to use this successfully on your sample data:

> z0= NULL
> for (i in 1:100){
+     z0[i]= 1
+ }
> z1= runif(100, min=0, max= 20)
> z2= runif(100, min=0, max=20)
> q0= runif(1, 0, 1)
> q1= runif(1, 0, 1)
> q2= runif(1, 0, 1)
> i= runif(1, 1, 101)
> i= ceiling(i-1)
> q= matrix(c(q0,q1,q2), ncol=3)
> z= matrix(c(z0[i],z1[i],z2[i]), ncol=3)
> t(q)%*%z
          [,1]     [,2]     [,3]
[1,] 0.3597998 3.227388 2.960053
[2,] 0.3544622 3.179510 2.916141
[3,] 0.3550781 3.185035 2.921208
> z%*%t(q)
         [,1]
[1,] 4.340265
Costanzo answered 4/9, 2015 at 15:10 Comment(0)
C
6

Sample Answer:

library(geometry)
dot(A,B)
Cone answered 29/10, 2020 at 0:8 Comment(0)
C
1

Since it seems like others have tackled your issue, I'll just add on to say that if you want a special dot product function, you can write one yourself:

dot <- function(x, y){   # x and y can be vectors or matrices
    result <- t(x)%*%y   # %*% is the matrix multiplication operator
    print(result)        # t(x) denotes the transpose of x
}

Or, as @user3503711 says in his answer, you can just use the dot() function from the geometry library.

Constrictor answered 25/4, 2021 at 17:30 Comment(1)
you probably want to return result instead of printing.Fecund

© 2022 - 2024 — McMap. All rights reserved.