Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (5.3k points)

I have a matrix (32X48).

How can I convert the matrix into a single-dimensional array?

1 Answer

0 votes
by
edited by

To convert a matrix to a 1-dimensional array, you can use the as.vector() function.

For example:

m=matrix(1:25,5,5)

> m

     [,1] [,2] [,3] [,4] [,5]

[1,]    1 6 11   16 21

[2,]    2 7 12   17 22

[3,]    3 8 13   18 23

[4,]    4 9 14   19 24

[5,]    5 10 15   20 25

as.vector(m)

 [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

You can use the transpose(t) function if you want the arrangement by columns:

as.vector(t(m))

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

Browse Categories

...