Convert a Matrix to a 1-Dimensional Array in R Programming
Write a R program to convert a matrix to a 1 dimensional array.
Sample Solution:
R Programming Code:
# Define row names for the matrix
row_names = c("row1", "row2", "row3", "row4")
# Define column names for the matrix
col_names = c("col1", "col2", "col3", "col4")
# Create a 4x4 matrix with values from 1 to 16, with specified row and column names
M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
# Print a message indicating the original matrix
print("Original Matrix:")
# Print the original matrix
print(M)
# Convert the matrix to a 1-dimensional array (column-wise)
result = as.vector(M)
# Print a message indicating the 1D array in column-wise order
print("1 dimensional array (column wise):")
# Print the 1-dimensional array (column-wise)
print(result)
# Convert the matrix to a 1-dimensional array (row-wise) by transposing it first
result = as.vector(t(M))
# Print a message indicating the 1D array in row-wise order
print("1 dimensional array (row wise):")
# Print the 1-dimensional array (row-wise)
print(result)
Output:
[1] "Original Matrix:" col1 col2 col3 col4 row1 1 2 3 4 row2 5 6 7 8 row3 9 10 11 12 row4 13 14 15 16 [1] "1 dimensional array (column wise):" [1] 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 [1] "1 dimensional array (row wise):" [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Explanation:
- Define Row Names:
- row_names = c("row1", "row2", "row3", "row4")
- Creates a vector of row names for the matrix.
- Define Column Names:
- col_names = c("col1", "col2", "col3", "col4")
- Creates a vector of column names for the matrix.
- Create Matrix:
- M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
- Constructs a 4x4 matrix with values from 1 to 16. The matrix is filled by rows (byrow = TRUE) and the specified row and column names are assigned.
- Print Original Matrix:
- print("Original Matrix:")
- Outputs a label for the original matrix.
- print(M)
- Displays the content of the original matrix.
- Convert to 1D Array (Column-Wise):
- result = as.vector(M)
- Converts the matrix to a 1-dimensional array, following the column-wise order.
- print("1 dimensional array (column wise):")
- Outputs a label for the column-wise 1D array.
- print(result)
- Displays the 1-dimensional array in column-wise order.
- Convert to 1D Array (Row-Wise):
- result = as.vector(t(M))
- Converts the matrix to a 1-dimensional array after transposing it (to change row-wise to column-wise) and then flattening.
- print("1 dimensional array (row wise):")
- Outputs a label for the row-wise 1D array.
- print(result)
- Displays the 1-dimensional array in row-wise order.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to extract the submatrix whose rows have column value > 7 from a given matrix.
Next: Write a R program to create a correlation matrix from a dataframe of same data type.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics