I have a 2x2x10 array of identity matrices, created with
arr = array(diag(2), dim=c(2,2,10))
I'm looking to multiply each 2x2 matrix within that array by a scalar c(1:10)
z = arr[,,1:10] * c(1:10)
However, I'm getting unexpected results. The first three 2x2 matrices of z
shown below
, , 1
[,1] [,2]
[1,] 1 0
[2,] 0 4
, , 2
[,1] [,2]
[1,] 5 0
[2,] 0 8
, , 3
[,1] [,2]
[1,] 9 0
[2,] 0 2
Am I missing something?
We need to rep
licate to make the lengths same
arr[,,1:10] * rep(1:10, each = length(arr[,, 1]))
or else 1 gets multiplied by the first element of arr[, , 1]
2 with the second element of arr[,, 1]
and due to recycling the elements of shorter vector is recycled until the length
of arr[, , 1:10]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With