8.7 C
New York
Sunday, July 12, 2020
No menu items!
Home Fun With Programming Programs How to find submatrices of a matrix in Python?

How to find submatrices of a matrix in Python?

Today, in this article we are going to tell about how to find submatrices of a matrix in python. there are two methods to extract submatrices of matrix. the first method is using a function and the second method is without using a function.

Sub matrix: suppose A is a given matrix. the matrix obtained by deleting some rows or columns of A is called a submatrix of A.

Using Function

Here we find submatrices of matrix using function.

def extractSubMatrix (
     matrix,
     rowStart, rowEnd,
     colStart, colEnd):
    
    result = [
        x[colStart : colEnd ]
        for x in matrix[ rowStart : rowEnd ]
        ]
    return result
matrix = [
    [1, 2, 3, 2],
    [4, 5, 6, 3],
    [7, 8, 8, 2],
    [4, 5, 1, 2],
    [1, 9, 6, 5],
    ]
print("matrix")
for array in matrix : print(array)
print()
SubMatrix = extractSubMatrix(matrix,
                             1, 5,
                             1, 3)
print("SubMatrix")
for array in SubMatrix: print(array)

OUTPUT:
matrix
[1, 2, 3, 2]
[4, 5, 6, 3]
[7, 8, 8, 2]
[4, 5, 1, 2]
[1, 9, 6, 5]
SubMatrix
[5, 6]
[8, 8]
[5, 1]
[9, 6]

Without Using Function

matrix = [[1,2,3],[2,3,4],[3,4,5]]
row = 3
col = 3
count = 0
x = 0
while x < row:
    y = x+1
    while y <= row:
        a = 0
        while a < col:
            b = a+1
            while b <= col:
                submatrix = []
                for i in matrix[x:y]:
                    submatrix.append(i[a:b])
                print(submatrix)
                count += 1
                b += 1
            a += 1
        y += 1
    x += 1

OUTPUT:

[[1]]
[[1, 2]]
[[1, 2, 3]]
[[2]]
[[2, 3]]
[[3]]
[[1], [2]]
[[1, 2], [2, 3]]
[[1, 2, 3], [2, 3, 4]]
[[2], [3]]
[[2, 3], [3, 4]]
[[3], [4]]
[[1], [2], [3]]
[[1, 2], [2, 3], [3, 4]]
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]
[[2], [3], [4]]
[[2, 3], [3, 4], [4, 5]]
[[3], [4], [5]]
[[2]]
[[2, 3]]
[[2, 3, 4]]
[[3]]
[[3, 4]]
[[4]]
[[2], [3]]
[[2, 3], [3, 4]]
[[2, 3, 4], [3, 4, 5]]
[[3], [4]]
[[3, 4], [4, 5]]
[[4], [5]]
[[3]]
[[3, 4]]
[[3, 4, 5]]
[[4]]
[[4, 5]]
[[5]]

Contributed By :- Priya Chauhan

Read More:-

5 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

4FansLike
281SubscribersSubscribe
- Advertisment -

Most Popular

How To Delete Facebook Account permanently?

Today we will know about one problem, which people keep talking about, today we will know how to delete Facebook account. With...

Serverless Computing – The Next Level of Server Architecture

In this article, we are going to explain the term Serverless computing. What it is and much more so let's start.

Best Tricks For Play & Win Ludo

Ludo King Game has now become the world's popular game. Everyone, children, old and young, likes to play this game.

Encrypt/Lock Your Pendrive with Password without any Software

How can you secure your pen drive without any software, you just have to follow some steps. After following these, you will...

Recent Comments