r - 当我在 R 中连接时,我是创建一行还是一列?

标签 r vector concatenation matrix-multiplication

我连接以下内容:

ExampleConCat <- c(1, 1, 1, 0)我有一个 20x4 矩阵(矩阵示例如下)。

我可以在 Rstudio 中进行矩阵乘法,如下所示:

matrix.multipl <- MatrixExample %*% ExampleConCat 

我得到以下结果:

#              [,1]
# cycle_1  0.99019608
# cycle_2  0.96400149
# cycle_3  0.91064055
# cycle_4  0.83460040
# cycle_5  0.74478532
# cycle_6  0.64981877
# cycle_7  0.55637987
# cycle_8  0.46893791
# cycle_9  0.39005264
# cycle_10 0.32083829
# cycle_11 0.26141338
# cycle_12 0.21127026
# cycle_13 0.16955189
# cycle_14 0.13524509
# cycle_15 0.10730721
# cycle_16 0.08474320
# cycle_17 0.06664783
# cycle_18 0.05222437
# cycle_19 0.04078855
# cycle_20 0.03176356

我的理解是:

要将 m×n 矩阵乘以 n×p 矩阵,ns 必须相同,结果是 m×p 矩阵。 https://www.mathsisfun.com/algebra/matrix-multiplying.html

因此,它的计算事实向我表明,上面的连接创建了一个列,即:MatrixExample 是一个 20X4 矩阵,因此 ExampleConCat 必须是一个 4X1 向量,以便这两个向量相互相乘。

或者,当向量乘以矩阵时是否有不同的规则,您能简单地向我解释一下吗?

当我尝试时我注意到

matrix.multipl <- ExampleConCat %*% MatrixExample 

我得到以下信息:

Error in ExampleConCat %*% MatrixExample : non-conformable arguments

我希望得到一个能够反射(reflect)我是 R 新手并且对矩阵乘法还较新的解释。

# MatrixExample:

#                State A   State B     State C     State D
# cycle_1  0.721453287 0.201845444 0.06689735 0.009803922
# cycle_2  0.520494846 0.262910628 0.18059602 0.035998510
# cycle_3  0.375512717 0.257831905 0.27729592 0.089359455
# cycle_4  0.270914884 0.225616773 0.33806874 0.165399604
# cycle_5  0.195452434 0.185784574 0.36354831 0.255214678
# cycle_6  0.141009801 0.147407084 0.36140189 0.350181229
# cycle_7  0.101731984 0.114117654 0.34053023 0.443620127
# cycle_8  0.073394875 0.086845747 0.30869729 0.531062087
# cycle_9  0.052950973 0.065278842 0.27182282 0.609947364
# cycle_10 0.038201654 0.048620213 0.23401643 0.679161707
# cycle_11 0.027560709 0.035963116 0.19788955 0.738586622
# cycle_12 0.019883764 0.026460490 0.16492601 0.788729740
# cycle_13 0.014345207 0.019389137 0.13581754 0.830448113
# cycle_14 0.010349397 0.014162175 0.11073351 0.864754914
# cycle_15 0.007466606 0.010318351 0.08952225 0.892692795
# cycle_16 0.005386808 0.007502899 0.07185350 0.915256795
# cycle_17 0.003886330 0.005447095 0.05731440 0.933352173
# cycle_18 0.002803806 0.003949642 0.04547092 0.947775632
# cycle_19 0.002022815 0.002860998 0.03590474 0.959211445
# cycle_20 0.001459366 0.002070768 0.02823342 0.968236444

最佳答案

如果你查看帮助部分help("%*%"),它简要描述了向量矩阵乘法的规则。

Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

在执行 MatrixExample %*% ExampleConCat 时,正如您正确指出的那样,符合这些规则,ExampleConCat 被视为 4 x 1 矩阵。但是,当 ExampleConCat %*% MatrixExample 完成时,尺寸不匹配,即 ExampleConCat 具有 4*1 (或 1* 4),而 MatrixExample 的维度为 20*4

向量将被转换为行或列矩阵,以矩阵起作用为准,示例如下:

exm = c(1,1,1,0)

exm_matrix = matrix(rnorm(16), 
                    ncol=4)

exm_matrix%*%exm
#>            [,1]
#> [1,]  2.1098758
#> [2,] -1.4432619
#> [3,] -0.2540392
#> [4,] -0.4211889

exm%*%exm_matrix
#>          [,1]       [,2]       [,3]      [,4]
#> [1,] 1.161164 -0.3602107 -0.3883783 -1.580562

reprex package 于 2021 年 7 月 2 日创建(v0.3.0)

关于r - 当我在 R 中连接时,我是创建一行还是一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68215912/

相关文章:

r - 很难将图例添加到我的图表中

c++ - 在结构中初始化空 vector - C++

vector 元素上的 C++ 调用函数,派生自 vector 的基类

file - 连接文件 - 包括文件夹中的文件

javascript - 如何将 2 个 JavaScript 数组连接成一个由这两个数组组合而成的新数组?

r - 在data.Table中使用反向选择按引用删除多列

R - 传单 - highcharter 工具提示(标签)

regex - 使用 knitr 和 LaTeX 为已编入索引的术语插入粗体文本

lambdas 的 C++ vector - undefined reference

javascript - 无法在 Javascript 中连接 String+Variable+String