Here we are explaining how to write an R program to convert a given matrix to a one-dimensional matrix. For that, 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 object into a vector.
as.vector(obj, mode = "any", proc.dest = "all")
where,
In this R program, we are printing the original matrix first and then we directly give the values to built-in functions. And print the function result. Here we used variables mtx, and x for assigning vector values and matrix.
STEP 1: Assign variable mtx with matrix values
STEP 2: First print the 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
mtx=matrix(1:12,3,4)
print("Original matrix is:")
print(mtx)
x = as.vector(mtx)
print("1 dimensional array is:")
print(x)
[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