This question is more about the MARGIN argument within the apply function than anything. Let's say that I want to multiply the following matrix by the following vector so that I multiply the first matrix element by the first vector element, second by second, and so on. I use the following code:
matrix <- matrix(1:10)
vector <- c(10:19)
t(apply(matrix,2,'*',vector))
which returns a very clean result:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 10 22 36 52 70 90 112 136 162 190
but if I change MARGIN = 2, I get this result:
matrix <- matrix(1:10)
vector <- c(10:19)
t(apply(matrix,1,'*',vector))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 10 11 12 13 14 15 16 17 18 19
[2,] 20 22 24 26 28 30 32 34 36 38
[3,] 30 33 36 39 42 45 48 51 54 57
[4,] 40 44 48 52 56 60 64 68 72 76
[5,] 50 55 60 65 70 75 80 85 90 95
[6,] 60 66 72 78 84 90 96 102 108 114
[7,] 70 77 84 91 98 105 112 119 126 133
[8,] 80 88 96 104 112 120 128 136 144 152
[9,] 90 99 108 117 126 135 144 153 162 171
[10,] 100 110 120 130 140 150 160 170 180 190
I suppose that if MARGIN = 1, the first matrix row will be multiplied by every vector element, then the second matrix row, etc., while MARGIN = 2 just multiplies the first matrix row by the first vector element?
Could somebody explain to me exactly what the difference is?
apply
? due to vectorization you can achieve the same with justt(matrix * vector)
. – SabDeM