[R] Matrix to Vector

John Kane jrkrideau at yahoo.ca
Sat Jun 5 20:54:14 CEST 2010


m<-matrix(seq(1,48),nrow=6,byrow=T)
as.vector(t(m))

gives me the correct result.  

Any chance you may have already transformed m ?

--- On Sat, 6/5/10, steven mosher <moshersteven at gmail.com> wrote:

> From: steven mosher <moshersteven at gmail.com>
> Subject: Re: [R] Matrix to Vector
> To: "Henrique Dallazuanna" <wwwhsd at gmail.com>
> Cc: r-help at r-project.org
> Received: Saturday, June 5, 2010, 2:44 PM
>  as.vector(t(m))
>  [1]  1  9 17 25 33 41  2 10 18 26 34
> 42  3 11 19 27 35 43  4 12 20 28 36 44
>  5 13 21 29 37 45  6 14 22 30 38 46  7 15 23 31
> 39 47  8 16 24
> [46] 32 40 48
> 
> the result I want is this:
> 
> [1]  1  2  3  4  5  6 
> 7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
> 24
> 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
> 45
> [46] 46 47 48
> 
> 
> 
> On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna
> <wwwhsd at gmail.com>wrote:
> 
> > Try this:
> >
> > as.vector(t(m))
> >
> > On Sat, Jun 5, 2010 at 3:12 PM, steven mosher <moshersteven at gmail.com>wrote:
> >
> >> Given a matrix of m*n, I want to reorder it as a
> vector, using a row major
> >> transpose.
> >>
> >> so:
> >>
> >> > m<-matrix(seq(1,48),nrow=6,byrow=T)
> >> > m
> >>     [,1] [,2] [,3] [,4] [,5]
> [,6] [,7] [,8]
> >> [1,]    1    2   
> 3    4    5    6   
> 7    8
> >> [2,]   
> 9   10   11   12   13   14   15   16
> >>
> [3,]   17   18   19   20   21   22   23   24
> >>
> [4,]   25   26   27   28   29   30   31   32
> >>
> [5,]   33   34   35   36   37   38   39   40
> >>
> [6,]   41   42   43   44   45   46   47   48
> >>
> >> I want to reorder this as a vector copying by row,
> so that the final
> >> vector
> >> has elements ordered thusly: row 1, column 1:N
> (m[1,1:n]) maps to
> >> row 1-n, and m[2,1:n] maps to row[n+1:2n] ...
> >>
> >> this obviously is not a solution: as the inherent
> column major storage
> >> paradigm of a matrix
> >> defeats the approach.
> >> > dim(m)<-c(48,1)
> >> > m
> >>      [,1]
> >>  [1,]    1
> >>  [2,]    9
> >>  [3,]   17
> >>  [4,]   25
> >>  [5,]   33
> >>  [6,]   41
> >>  [7,]    2
> >>  [8,]   10
> >>  [9,]   18
> >> [10,]   26
> >> [11,]   34
> >> [12,]   42
> >> [13,]    3
> >> [14,]   11
> >> [15,]   19
> >> [16,]   27
> >> [17,]   35
> >> [18,]   43
> >> [19,]    4
> >> [20,]   12
> >> [21,]   20
> >> [22,]   28
> >> [23,]   36
> >> [24,]   44
> >> [25,]    5
> >> [26,]   13
> >> [27,]   21
> >> [28,]   29
> >> [29,]   37
> >> [30,]   45
> >> [31,]    6
> >> [32,]   14
> >> [33,]   22
> >> [34,]   30
> >> [35,]   38
> >> [36,]   46
> >> [37,]    7
> >> [38,]   15
> >> [39,]   23
> >> [40,]   31
> >> [41,]   39
> >> [42,]   47
> >> [43,]    8
> >> [44,]   16
> >> [45,]   24
> >> [46,]   32
> >> [47,]   40
> >> [48,]   48
> >>
> >>
> >> I already have a version that loops through the
> data ( this is actually a
> >> portion of a data frame ) to reorder
> >> this into a vector, but I was hoping there was an
> elegant way
> >>
> >>        [[alternative HTML
> version deleted]]
> >>
> >> ______________________________________________
> >> R-help at r-project.org
> mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained,
> reproducible code.
> >>
> >
> >
> >
> > --
> > Henrique Dallazuanna
> > Curitiba-Paraná-Brasil
> > 25° 25' 40" S 49° 16' 22" O
> >
> 
>     [[alternative HTML version deleted]]
> 
> 
> -----Inline Attachment Follows-----
> 
> ______________________________________________
> R-help at r-project.org
> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
> 





More information about the R-help mailing list