R中矩阵的两列相乘的和

mrn*_*mrn 3 r matrix dot-product

我使用以下方法在R中生成矩阵,

ncolumns = 3
nrows = 10
my.mat <- matrix(runif(ncolumns*nrows), ncol=ncolumns)
Run Code Online (Sandbox Code Playgroud)

该矩阵表示3D中点的坐标.如何在R中计算以下?

sum of x(i)*y(i)
Run Code Online (Sandbox Code Playgroud)

例如,如果矩阵是,

x y z
1 2 3
4 5 6
Run Code Online (Sandbox Code Playgroud)

然后输出= 1*2 + 4*5

我正在努力学习R.所以任何帮助都会非常感激.

谢谢

N8T*_*TRO 6

您正在寻找%*%函数.

ncolumns = 3
nrows = 10

my.mat <- matrix(runif(ncolumns*nrows), ncol=ncolumns)

(my.answer <- my.mat[,1] %*% my.mat[,2])

#       [,1]
# [1,] 1.519
Run Code Online (Sandbox Code Playgroud)