Question

Must be in Python(please have a screen shot on these solution code)11.5 (Algebra: add two matrices) Write a function to add two matrices. The header of the function is def addMatrix(a, b): In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element Cij is aij + bij. For example, for two 3 X 3 matrices a and b, c is Programming Exercises 383 11 12 13 13 a31 a32 a33 a3 b a32 b32 a33 +b33 Write a test program that prompts the user to enter two 3 X 3 matrices and dis- plays their sum. Here is a sample run Enter matrix1: 1 2 3 4 5 6 7 8 9 Enter Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 The matrices are added as follows: Enter 0.0 2.0 4. 4.0 5.0 6.01.0 4.5 2.2 5.0 11.5 8.2 1.1 4.3 5.2 1.0 2.0 3.0 1.0 4.0 11.0 11.0 8.0 11.0 8.1 12.3 14.2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def addMatrix(a , b):

    # create a matrix of same dimension as a and b

    c = [ [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]

   

    for i in range( 0 , len(a) ):

   

        for j in range( 0 , len( a[i] ) ):

       

            c[i][j] = a[i][j] + b[i][j]

           

    return c

print('Enter 3x3 matrix')

   

x = list( map( float , input('Enter matrix 1 : ').split(' ') ) )

y = list( map( float , input('Enter matrix 2 : ').split(' ') ) )

index = 0

a = [ [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]

b = [ [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]

for i in range( 0 , 3 ):

   

    for j in range( 0 , 3 ):

   

        a[i][j] = x[index]

        

        index += 1

       

index = 0

for i in range( 0 , 3 ):

   

    for j in range( 0 , 3 ):

   

        b[i][j] = y[index]

       

        index += 1

c = addMatrix( a , b )

for i in range(0 , 3):

    print(a[0][i] , end = ' ')

   

print('   ' , end = '')

for i in range(0 , 3):

    print(b[0][i] , end = ' ')

print(' ', end = '')

   

for i in range(0 , 3):

    print(c[0][i] , end = ' ')

   

print()

   

for i in range(0 , 3):

    print(a[1][i] , end = ' ')

   

print(' + ' , end = '')

   

for i in range(0 , 3):

    print(b[1][i] , end = ' ')

   

print(' = ', end = '')

   

for i in range(0 , 3):

    print(c[1][i] , end = ' ')

   

print()

for i in range(0 , 3):

    print(a[2][i] , end = ' ')

   

print('   ' , end = '')

for i in range(0 , 3):

    print(b[2][i] , end = ' ')

print(' ', end = '')

   

for i in range(0 , 3):

    print(c[2][i] , end = ' ')

Sample Output

Enter 3x3 matrix Enter matrix 11 2 3 4 5 678 9 Enter matrix 2 : 2 4 1 4.5 2.2 1.1 4.3 5. 1.0 2.0 3. 4.0 5. 6.0 + 1. 4.5 2.25

Add a comment
Know the answer?
Add Answer to:
Must be in Python(please have a screen shot on these solution code) 11.5 (Algebra: add two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In a digital communication system, probability density function of the two level signal received in the...

    In a digital communication system, probability density function of the two level signal received in the receiver is: PR(v) = PS(v)*PN(v) = [0.4δ(v+1) + 0.6δ(v-4)]*η(v). And , η(v) is the noise that added to the message sign as the additive Gaussian noise with a value of zero and an effective value of 3. (* symbol means convolution process, in the solution of this problem you can use the below Q function table.) ,   η(v) = A) Plot the probability density...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT