Search code examples
rmatrix-multiplicationrowwise

multiply a vector of numbers with matrix rowwise


Consider a vector of numbers , a <- c(75,26,65,27,97,72)

And a matrix 10x6 matrix 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

dput

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))

My question is how do I multiply the vector a with matrix b, row wise. I know what b%*%a will do.

I am trying to do something like this

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

so on

Any suggestions are much appreciated.


Solution

  • Looks like a sweep-operation. In R for functions applied to a marging, "2" generally indicates a column operation, which from your argument and structures is how I would describe your expected result. n(I can see how you would call this "row-wise" but most R users would think of this as being applied "column-wise:.

    > 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
    

    Then just 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
    

    Alternatively, the matrix operation:

      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
    

    And the slightly faster single function version:

    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