I have a 10 by 6 matrix and I'd like to multiply columns 2:6 by column 1, then columns 3:6 by column 2, and so on until I'm left with column 5 * column 6. The matrix is below.
M12.7 <- matrix(data = 1:60, nrow = 10, ncol = 6)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 11 21 31 41 51
[2,] 2 12 22 32 42 52
[3,] 3 13 23 33 43 53
[4,] 4 14 24 34 44 54
[5,] 5 15 25 35 45 55
[6,] 6 16 26 36 46 56
[7,] 7 17 27 37 47 57
[8,] 8 18 28 38 48 58
[9,] 9 19 29 39 49 59
[10,] 10 20 30 40 50 60
This should result in 5 matrices for a total of 15 columns. I want to then combine those matrices into a 10 by 15 matrix. I've written a function that sort of gets me there. But I can't apply it to a whole reference matrix, M12.7 in this case. The function is:
function(datamatrix, column, colz) {
datamatrix[, column] * datamatrix[, (column 1):colz]
}
datamatrix in the function is not data.matrix. Any help is greatly appreciated
CodePudding user response:
s <- seq(ncol(M12.7)-1)
do.call(cbind, lapply(s, function(x)M12.7[,x]*M12.7[, -seq(x)]))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 11 21 31 41 51 231 341 451 561 651 861 1071 1271 1581 2091
[2,] 24 44 64 84 104 264 384 504 624 704 924 1144 1344 1664 2184
[3,] 39 69 99 129 159 299 429 559 689 759 989 1219 1419 1749 2279
[4,] 56 96 136 176 216 336 476 616 756 816 1056 1296 1496 1836 2376
[5,] 75 125 175 225 275 375 525 675 825 875 1125 1375 1575 1925 2475
[6,] 96 156 216 276 336 416 576 736 896 936 1196 1456 1656 2016 2576
[7,] 119 189 259 329 399 459 629 799 969 999 1269 1539 1739 2109 2679
[8,] 144 224 304 384 464 504 684 864 1044 1064 1344 1624 1824 2204 2784
[9,] 171 261 351 441 531 551 741 931 1121 1131 1421 1711 1911 2301 2891
[10,] 200 300 400 500 600 600 800 1000 1200 1200 1500 1800 2000 2400 3000
Another way:
s <- seq(ncol(M12.7) -1)
n <- sequence(rev(s)) 1
p <- cumsum(n==2)
M12.7[, p] *M12.7[, n p-1]
CodePudding user response:
If I'm understanding correctly, we can use the cumulative product cumprod
function applied to each row:
t(apply(M12.7, MARGIN = 1, FUN = cumprod))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 11 231 7161 293601 14973651
[2,] 2 24 528 16896 709632 36900864
[3,] 3 39 897 29601 1272843 67460679
[4,] 4 56 1344 45696 2010624 108573696
[5,] 5 75 1875 65625 2953125 162421875
[6,] 6 96 2496 89856 4133376 231469056
[7,] 7 119 3213 118881 5587407 318482199
[8,] 8 144 4032 153216 7354368 426553344
[9,] 9 171 4959 193401 9476649 559122291
[10,] 10 200 6000 240000 12000000 720000000
CodePudding user response:
There is rowCumprods
in matrixStats
library(matrixStats)
rowCumprods(M12.7)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 11 231 7161 293601 14973651
[2,] 2 24 528 16896 709632 36900864
[3,] 3 39 897 29601 1272843 67460679
[4,] 4 56 1344 45696 2010624 108573696
[5,] 5 75 1875 65625 2953125 162421875
[6,] 6 96 2496 89856 4133376 231469056
[7,] 7 119 3213 118881 5587407 318482199
[8,] 8 144 4032 153216 7354368 426553344
[9,] 9 171 4959 193401 9476649 559122291
[10,] 10 200 6000 240000 12000000 720000000