将数字向量与矩阵逐行相乘

时间:2018-11-22 19:04:03

标签: r matrix-multiplication rowwise

考虑数字向量a <- c(75,26,65,27,97,72)

还有一个矩阵10x6矩阵b

1.4168709   0.6253624   2.08645202  2.9475645   1.29317931  0.80175442
0.3669328   0.851852    0.57428245  2.8542504   1.40075478  0.01745655
6.1173956   1.6848444   1.05468424  0.3382552   1.1428774   0.41141215
2.8203602   0.9573334   0.22131122  0.4406137   0.07209113  0.17910147
0.102152    0.1779387   0.94915127  0.3516491   1.48272109  0.06037996
0.3124434   0.4892484   2.04443039  0.1251463   2.41507973  1.25367433
0.2154152   0.3951161   0.60410084  0.7551265   0.55764737  1.17793564
1.5451135   0.7764766   3.11515773  1.3519765   0.08916275  1.39969422
0.4018092   0.2432501   0.06470464  2.6173665   0.24696145  5.27272096
1.1683212   0.1258633   0.19431636  0.4160356   1.61775945  0.78849181

投放

b <- structure(c(1.41687091749774, 0.366932780481875, 6.11739562418232, 
2.8203601760972, 0.102152034174651, 0.312443420290947, 0.215415194164962, 
1.54511345728281, 0.401809234172106, 1.16832122397808, 0.625362366437912, 
0.851851973640633, 1.68484436153414, 0.957333435262454, 0.177938693314666, 
0.489248352590948, 0.395116138737649, 0.776476616387118, 0.243250062223524, 
0.125863284132781, 2.08645202020619, 0.57428245106712, 1.05468423915856, 
0.221311220899224, 0.949151266561806, 2.04443038991633, 0.604100843891501, 
3.11515773070936, 0.0647046443940286, 0.194316359037562, 2.94756450172152, 
2.85425036383753, 0.338255227074493, 0.440613748457464, 0.351649099495262, 
0.125146273523569, 0.755126529331219, 1.35197646259786, 2.61736654663894, 
0.416035552509129, 1.29317931454153, 1.40075477585735, 1.14287740174205, 
0.072091125883162, 1.48272109049815, 2.41507973323081, 0.557647368015562, 
0.0891627511009574, 0.246961451135576, 1.61775945491138, 0.80175441955164, 
0.0174565480835137, 0.411412146408111, 0.179101474117488, 0.0603799588836676, 
1.25367433010839, 1.17793564121695, 1.39969422101023, 5.27272095591089, 
0.788491813423944), .Dim = c(10L, 6L))

我的问题是如何将向量a与矩阵b按行相乘。我知道b%*%a会做什么。

我正在尝试做这样的事情

75*1.4168709 + 26*0.6253624 + 65*2.08645202 + 27*2.9475645 + 97*1.29317931 + 72*0.80175442

75*0.3669328 + 26*0.851852 + 65*0.57428245 + 27*2.8542504 + 97*1.40075478 +     72*0.01745655

等等

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

在乘法之前,我们可以得到相同的长度,即通过复制'a'元素

a[col(b)] * b
#          [,1]      [,2]       [,3]      [,4]       [,5]       [,6]
# [1,] 106.265319 16.259422 135.619381 79.584242 125.438394  57.726318
# [2,]  27.519959 22.148151  37.328359 77.064760 135.873213   1.256871
# [3,] 458.804672 43.805953  68.554476  9.132891 110.859108  29.621675
# [4,] 211.527013 24.890669  14.385229 11.896571   6.992839  12.895306
# [5,]   7.661403  4.626406  61.694832  9.494526 143.823946   4.347357
# [6,]  23.433257 12.720457 132.887975  3.378949 234.262734  90.264552
# [7,]  16.156140 10.273020  39.266555 20.388416  54.091795  84.811366
# [8,] 115.883509 20.188392 202.485252 36.503364   8.648787 100.777984
# [9,]  30.135693  6.324502   4.205802 70.668897  23.955261 379.635909
#[10,]  87.624092  3.272445  12.630563 11.232960 156.922667  56.771411

或转置'b',然后与'a'相乘并转置输出

t(t(b) * a)
#          [,1]      [,2]       [,3]      [,4]       [,5]       [,6]
# [1,] 106.265319 16.259422 135.619381 79.584242 125.438394  57.726318
# [2,]  27.519959 22.148151  37.328359 77.064760 135.873213   1.256871
# [3,] 458.804672 43.805953  68.554476  9.132891 110.859108  29.621675
# [4,] 211.527013 24.890669  14.385229 11.896571   6.992839  12.895306
# [5,]   7.661403  4.626406  61.694832  9.494526 143.823946   4.347357
# [6,]  23.433257 12.720457 132.887975  3.378949 234.262734  90.264552
# [7,]  16.156140 10.273020  39.266555 20.388416  54.091795  84.811366
# [8,] 115.883509 20.188392 202.485252 36.503364   8.648787 100.777984
# [9,]  30.135693  6.324502   4.205802 70.668897  23.955261 379.635909
#[10,]  87.624092  3.272445  12.630563 11.232960 156.922667  56.771411

或者reprep明确关联

rep(a, each = nrow(b)) * b
#           [,1]      [,2]       [,3]      [,4]       [,5]       [,6]
# [1,] 106.265319 16.259422 135.619381 79.584242 125.438394  57.726318
# [2,]  27.519959 22.148151  37.328359 77.064760 135.873213   1.256871
# [3,] 458.804672 43.805953  68.554476  9.132891 110.859108  29.621675
# [4,] 211.527013 24.890669  14.385229 11.896571   6.992839  12.895306
# [5,]   7.661403  4.626406  61.694832  9.494526 143.823946   4.347357
# [6,]  23.433257 12.720457 132.887975  3.378949 234.262734  90.264552
# [7,]  16.156140 10.273020  39.266555 20.388416  54.091795  84.811366
# [8,] 115.883509 20.188392 202.485252 36.503364   8.648787 100.777984
# [9,]  30.135693  6.324502   4.205802 70.668897  23.955261 379.635909
#[10,]  87.624092  3.272445  12.630563 11.232960 156.922667  56.771411

或者我们可以按列split将矩阵“ b”列为list,并将其与mapply一起使用。现在,将对应的单个单位相乘

mapply(`*`, split(b, col(b)), a)

完成上述步骤后,只需执行rowSums

out2 <- rowSums(a[col(b)] * b)
out2
#[1] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541

-使用OP的方法检查输出

out1 <- (b%*%a)[,1]
out1
#[1] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541
all.equal(out1, out2)
#[1] TRUE

答案 1 :(得分:0)

看起来像一个sweep操作。在R中,对于应用于页边距的函数,“ 2”通常表示列操作,根据您的论点和结构,我将用它描述您的预期结果。 n(我可以看到您如何称呼它为“行向”,但大多数R用户会认为这是“行向:”。

> sweep(b,2,a,"*")
            [,1]      [,2]       [,3]      [,4]       [,5]       [,6]
 [1,] 106.265319 16.259422 135.619381 79.584242 125.438394  57.726318
 [2,]  27.519959 22.148151  37.328359 77.064760 135.873213   1.256871
 [3,] 458.804672 43.805953  68.554476  9.132891 110.859108  29.621675
 [4,] 211.527013 24.890669  14.385229 11.896571   6.992839  12.895306
 [5,]   7.661403  4.626406  61.694832  9.494526 143.823946   4.347357
 [6,]  23.433257 12.720457 132.887975  3.378949 234.262734  90.264552
 [7,]  16.156140 10.273020  39.266555 20.388416  54.091795  84.811366
 [8,] 115.883509 20.188392 202.485252 36.503364   8.648787 100.777984
 [9,]  30.135693  6.324502   4.205802 70.668897  23.955261 379.635909
[10,]  87.624092  3.272445  12.630563 11.232960 156.922667  56.771411

然后只是rowSums

> rowSums( sweep(b,2,a,"*") )
 [1] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541

或者,矩阵运算:

  a %*% t(b)
         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
[1,] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541

单功能版本稍快一些:

tcrossprod(a,b)
         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
[1,] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541