在R中添加矩阵的元素

时间:2016-07-28 16:15:17

标签: r matrix

我有两个矩阵如下:

loweredge 
       [,1]  [,2] [,3]
  [1,] -32.5 87.5 207.5

SectorAzimuth
       [,1]  [,2]  [,3] [,4]  [,5]  [,6]
 [1,]  10.83 21.66 32.5 43.33 54.16  65

我希望将SectorAzimuth中的所有6个值添加到lowerge的第一个值,然后将SectorAzimuth中的所有6个值添加到lowerge的第二个值,并将SectorAzimuth的相同6个值添加到lowerge的第3个值。

有人可以给我一些指示吗?

1 个答案:

答案 0 :(得分:1)

你可以尝试

loweredge <- matrix(c(-32.5, 87.5, 207.5), nrow = 1)
SectorAzimuth <- matrix(c(10.83, 21.66, 32.5, 43.33, 54.16,  65), nrow = 1)

apply(loweredge, MARGIN = 2, FUN = `+`, y = SectorAzimuth)
       [,1]   [,2]   [,3]
[1,] -21.67  98.33 218.33
[2,] -10.84 109.16 229.16
[3,]   0.00 120.00 240.00
[4,]  10.83 130.83 250.83
[5,]  21.66 141.66 261.66
[6,]  32.50 152.50 272.50