R Program to convert a given matrix to a one dimensional

How to convert a given matrix to a one dimensional

Here we are explaining how to write an R program to convert a given matrix to a one-dimensional matrix. Here we are using a built-in function as.vector() for this conversion. This method helps to converts a distributed matrix into a non-distributed vector. Also, this method helps to convert any objects into a vector.

as.vector(obj, mode = "any", proc.dest = "all")
 

obj: Any obj and returns the vector or tries to coerce the obj into a vector of mode.

mode: Character string giving an atomic mode or “list“, or “any”.

proc.dest: Destination process for storing the matrix.

How to convert a given matrix to a one-dimensional in R Program

Below are the steps used in the R program to convert a given matrix to a one-dimensional one. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variables mtx,x for assigning vector values and matrix.

ALGORITHM

STEP 1: Assign variable mtx with matrix values

STEP 2: First print original matrix values

STEP 3: Convert the matrix using as.vector()

STEP 4: Assign the result array into variable x

STEP 5: print the result of the function as one dimensional array

 

R Source Code

                                          mtx=matrix(1:12,3,4)
print("Original matrix is:")
print(mtx)
x = as.vector(mtx)
print("1 dimensional array is:")
print(x)

                                      

OUTPUT

[1] "Original matrix is"
[,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
[1] "1 dimensional array is:"
 [1]  1  2  3  4  5  6  7  8  9 10 11 12