Convert matrix to one-dimensional array

I have a matrix (32X48).

How to convert a matrix to a one-dimensional array?

+59
arrays matrix r
Sep 29 '10 at 15:38
source share
10 answers

If we are talking about data.frame, then you should ask yourself if the variables are of the same type? If this is the case, you can use rapply or unlist, since data.frames are lists, deep in their souls ...

data(mtcars) unlist(mtcars) rapply(mtcars, c) # completely stupid and pointless, and slower 
+27
Sep 29 '10 at 17:15
source share

Either read it using "scan", or just do as.vector () on the matrix. You can move the matrix first if you want it in rows or columns. The solutions published so far are so rude that I’m not even going to try them ...

 > m=matrix(1:12,3,4) > m [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > as.vector(m) [1] 1 2 3 4 5 6 7 8 9 10 11 12 > as.vector(t(m)) [1] 1 4 7 10 2 5 8 11 3 6 9 12 
+148
Sep 29 '10 at 16:23
source share

try c()

 x = matrix(1:9, ncol = 3) x [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 c(x) [1] 1 2 3 4 5 6 7 8 9 
+23
Sep 29 '10 at 17:15
source share

array(A) or array(t(A)) will give you the 1st array.

+9
Feb 20 2018-12-12T00:
source share

From ?matrix Matrix: “A matrix is ​​a special case of a two-dimensional“ array. ”You can simply resize the matrix / array.

 Elts_int <- as.matrix(tmp_int) # read.table returns a data.frame as Brandon noted dim(Elts_int) <- (maxrow_int*maxcol_int,1) 
+8
Sep 29 '10 at 15:49
source share

Perhaps so late, anyway, this is my way of converting a matrix into a vector:

 library(gdata) vector_data<- unmatrix(yourdata,byrow=T)) 

hope this helps

+5
Nov 25 '13 at 0:43
source share

You can use Joshua's solution, but I think you need Elts_int <- as.matrix(tmp_int)

Or for loops:

 z <- 1 ## Initialize counter <- 1 ## Initialize for(y in 1:48) { ## Assuming 48 columns otherwise, swap 48 and 32 for (x in 1:32) { z[counter] <- tmp_int[x,y] counter <- 1 + counter } } 

z is a 1d vector.

+1
Sep 29 '10 at 16:18
source share

Simple and fast, since a 1d array is essentially a vector

 vector <- array[1:length(array)] 
+1
May 03 '13 at 21:27
source share

If you instead have data.frame (df) that had multiple columns and you want you to vectorize, you can do

as.matrix (df, ncol = 1)

+1
Dec 20 '16 at 0:02
source share

you can use as.vector() . It seems like this is the fastest method according to my small benchmark, as follows:

 library(microbenchmark) x=matrix(runif(1e4),100,100) # generate a 100x100 matrix microbenchmark(y<-as.vector(x),y<-x[1:length(x)],y<-array(x),y<-c(x),times=1e4) 

The first solution uses as.vector() , the second uses the fact that the matrix is ​​stored as a continuous array in memory, and length(m) gives the number of elements in the matrix m . The third instance of array from x , and the fourth uses the concatenate c() function. I also tried unmatrix from gdata , but it is mentioned too slowly here.

Here are some of our numerical results:

 > microbenchmark( y<-as.vector(x), y<-x[1:length(x)], y<-array(x), y<-c(x), times=1e4) Unit: microseconds expr min lq mean median uq max neval y <- as.vector(x) 8.251 13.1640 29.02656 14.4865 15.7900 69933.707 10000 y <- x[1:length(x)] 59.709 70.8865 97.45981 73.5775 77.0910 75042.933 10000 y <- array(x) 9.940 15.8895 26.24500 17.2330 18.4705 2106.090 10000 y <- c(x) 22.406 33.8815 47.74805 40.7300 45.5955 1622.115 10000 

Matrix smoothing is a common operation in Machine Learning, where the matrix can represent parameters for study, but uses an optimization algorithm from a common library that expects a parameter vector. Therefore, it is customary to convert a matrix (or matrices) into such a vector. This is the case with the standard optim() R function.

+1
Sep 08 '17 at 16:18
source share



All Articles