Convert a matrix to vector and back
You must use byrow = FALSE (the default) to convert the vector back to a matrix.
> (d <- matrix(1:6, nrow=2, ncol=3))
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> (v <- as.vector(d))
[1] 1 2 3 4 5 6
> matrix(v, nrow=2, ncol=3, byrow = FALSE)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> (v <- as.vector(d))
[1] 1 2 3 4 5 6
> matrix(v, nrow=2, ncol=3, byrow = FALSE)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Commentaires
Enregistrer un commentaire