Question

How do I generate a banded matrix in Python programming language? I need the function of...

How do I generate a banded matrix in Python programming language? I need the function of code required to do this

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

In case of any query, do comment. Please rate answer. Thanks

A matrix of given size is banded matrix where

bandedMatrix(i,j) = 0 , If (j < i-k1) or (j > i +k2), else put some number

here k1>=0 and k1 is lower bandwidth,

k2 >=0 and k2 is upper bandwidth. Maximum of k1 and k2 is called bandwidth of the matrix.

For example here we consider k1=k2.

So condition for banded matrix would be |i-j| > k

Please use below code:

#function to generate Banded matrix

def GenerateBandedMatrix(size,bandwidth):

    # Initialize the matrix of size*size

    matrix = [[0 for row in range(size)] for col in range(size)]

    count =1

    # iterate over matrix to create a banded matrix

    for i in range(size):

        for j in range(size):

            #if |i-j| > size then leave element of matrix as 0

            if(abs(i-j) > bandwidth):

                continue;

          

            # update the matrix with count and increase the count

            matrix[i][j] = count

            count +=1

          

    return matrix

# Driver code to call GenerateBandedMatrix

size =6

bandwidth =2

print('The banded Matrix is as below: ')

bandedMatrix = GenerateBandedMatrix(size,bandwidth)

#print the generated matrix

for i in range(size):

    for j in range(size):

        print(bandedMatrix[i][j], end ='')

        print(' ', end='')

    print('')

=============screen shot of the code ==================

Output:

Add a comment
Know the answer?
Add Answer to:
How do I generate a banded matrix in Python programming language? I need the function of...
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
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