There is another "matrix" strategy that succeeds, although it is clearly less economical that the transpose approach:

matrix(mat[7:1, ], ncol=nrow(mat), byrow=TRUE) # will transpose the matrix

I offer this only as a reminder that the byrow= parameter is available when appropriate.

--
David.


On Aug 23, 2009, at 3:18 PM, (Ted Harding) wrote:

The problem with David's proposal is revealed by:

 mat[7:1,]
 #      [,1] [,2] [,3]
 # [1,]    7   14   21
 # [2,]    6   13   20
 # [3,]    5   12   19
 # [4,]    4   11   18
 # [5,]    3   10   17
 # [6,]    2    9   16
 # [7,]    1    8   15

which simply reverses the rows. Then:

 c(mat[7:1,])
 # [1]  7  6  5  4  3  2  1 14 13 12 11 10  9  8 21 20 19 18 17 16 15

since a matrix is stored "by columns" -- i.e. as a vector with its
elements ordered down each column, per col1 then col2 then ...

And the following won't work either:

 mat[,1:3]
 #      [,1] [,2] [,3]
 # [1,]    1    8   15
 # [2,]    2    9   16
 # [3,]    3   10   17
 # [4,]    4   11   18
 # [5,]    5   12   19
 # [6,]    6   13   20
 # [7,]    7   14   21

which is simply the original matrix, and hence:

 c(mat[,1:3])
 # [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21

for the same reson as before. What you need to do is based on the
following:

 t(mat[7:1,])
 #      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
 # [1,]    7    6    5    4    3    2    1
 # [2,]   14   13   12   11   10    9    8
 # [3,]   21   20   19   18   17   16   15

which now has the elements (down the columns) in the order you want.
So:

 c(t(mat[7:1,]))
 # [1]  7 14 21  6 13 20  5 12 19  4 11 18  3 10 17  2  9 16  1  8 15

As desired.
Ted.


On 23-Aug-09 18:53:38, Bogaso wrote:

No no, I actually want following result :

7,   14,   21, 6,   13,   20, 5,   12,   19,............




David Winsemius wrote:


On Aug 23, 2009, at 2:37 PM, Bogaso wrote:


I have suppose a matrix like that

mat <- matrix(1:21, 7)
mat
   [,1] [,2] [,3]
[1,]    1    8   15
[2,]    2    9   16
[3,]    3   10   17
[4,]    4   11   18
[5,]    5   12   19
[6,]    6   13   20
[7,]    7   14   21

From this matrix, I want to create a vector like tha :

c(mat[7,], mat[6,], mat[5,], ....., mat[1,])

Can anyone please guide me, how to do that?

 c( mat[7:1,] )

# [1]  7  6  5  4  3  2  1 14 13 12 11 10  9  8 21 20 19 18 17 16 15

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help@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.



--
View this message in context:
http://www.nabble.com/A-matrix-calculation-tp25106048p25106224.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.hard...@manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Aug-09                                       Time: 20:17:58
------------------------------ XFMail ------------------------------

______________________________________________
R-help@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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to