w3resource

Create an array of Two 3x3 Matrices from Vectors in R

R Programming: Array Exercise-4 with Solution

Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns from two given two vectors. Print the second row of the second matrix of the array and the element in the 3rd row and 3rd column of the 1st matrix.

Sample Solution :

R Programming Code :

# Print a message indicating that two vectors of different lengths will be used
print("Two vectors of different lengths:")

# Define the first vector with elements 1, 3, 4, and 5
v1 =  c(1,3,4,5)

# Define the second vector with elements 10, 11, 12, 13, 14, and 15
v2 =  c(10,11,12,13,14,15)

# Print the first vector to display its contents
print(v1)

# Print the second vector to display its contents
print(v2)

# Create a 3-dimensional array using the vectors, with dimensions 3x3x2
result = array(c(v1,v2), dim = c(3,3,2))

# Print a message indicating that the new array will be displayed
print("New array:")

# Print the 3-dimensional array to show its contents
print(result)

# Print a message indicating that the second row of the second matrix in the array will be shown
print("The second row of the second matrix of the array:")

# Print the second row of the second matrix in the array
print(result[2,,2])

# Print a message indicating that a specific element of the first matrix will be shown
print("The element in the 3rd row and 3rd column of the 1st matrix:")

# Print the element located in the 3rd row and 3rd column of the first matrix in the array
print(result[3,3,1])

Output:

[1] "Two vectors of different lengths:"
[1] 1 3 4 5
[1] 10 11 12 13 14 15
[1] "New array:"
, , 1

     [,1] [,2] [,3]
[1,]    1    5   12
[2,]    3   10   13
[3,]    4   11   14

, , 2

     [,1] [,2] [,3]
[1,]   15    4   11
[2,]    1    5   12
[3,]    3   10   13

[1] "The second row of the second matrix of the array:"
[1]  1  5 12
[1] "The element in the 3rd row and 3rd column of the 1st matrix:"
[1] 14                         

Explanation:

  • The code prints a message indicating that it will deal with two vectors of different lengths.
  • It defines vector v1 with elements 1, 3, 4, and 5, and vector v2 with elements 10, 11, 12, 13, 14, and 15.
  • The code then prints both vectors to display their contents.
  • Next, it creates a 3-dimensional array called result by combining v1 and v2, setting the dimensions of the array to 3x3x2.
  • It prints a message indicating that the new array will be displayed.
  • The code prints the 3-dimensional array, showing its structure.
  • It prints a message and then displays the second row of the second matrix in the array.
  • Finally, it prints a message and shows the element located in the 3rd row and 3rd column of the first matrix in the array.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create an 3 dimensional array of 24 elements using the dim() function.
Next: Write a R program to combine three arrays so that the first row of the first array is followed by the first row of the second array and then first row of the third array.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/array/r-programming-array-exercise-4.php