Write a R program to create two 2x3 matrix and add, subtract, multiply and divide the matrixes

  • برمجة R

Write a R program to create two 2x3 matrix and add, subtract, multiply and divide the matrixes.

الأجوبة

# Create two 2x3 matrixes.
m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(m1)
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(m2)

result = m1 + m2
print("Result of addition")
print(result)

result = m1 - m2
print("Result of subtraction")
print(result)

result = m1 * m2
print("Result of multiplication")
print(result)

result = m1 / m2
print("Result of division:")
print(result)

Sample Output:

[1] "Matrix-1:"
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[1] "Matrix-2:"
     [,1] [,2] [,3]
[1,]    0    2    0
[2,]    1    3    2
[1] "Result of addition"
     [,1] [,2] [,3]
[1,]    1    5    5
[2,]    3    7    8
[1] "Result of subtraction"
     [,1] [,2] [,3]
[1,]    1    1    5
[2,]    1    1    4
[1] "Result of multiplication"
     [,1] [,2] [,3]
[1,]    0    6    0
[2,]    2   12   12
[1] "Result of division:"
     [,1]     [,2] [,3]
[1,]  Inf 1.500000  Inf
[2,]    2 1.333333    3
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...