Consider A 2 dimensional matrix Of size MxN

Share

Consider A 2 dimensional matrix Of size MxN Consisting of both positive and negative values.Identify the largest Sub matrix whose values are All positive.

Input 

[-1   -2

3    4]

Output

[3    4]


extract sub matrices Using Python,Method to extract submatrix from numpy matrix,submatrices of a Given Matrix, how get sub matrix in python


import numpy as np
a = np.array([[-1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, -16]])
# Extract all possible sub-arrays of a
sub_arrays = []
if not (a< 0).all():
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            for k in range(i+1, a.shape[0]+1):
                for l in range(j+1, a.shape[1]+1):
                    if (a[i:k, j:l] >= 0).all():
                        sub_arrays.append(a[i:k, j:l])
if not sub_arrays==[]:
    # Find the maximum shape
    max_shape = 0
    for array in sub_arrays:
        if max_shape is None or array.size > max_shape:
            max_shape = array.size
    # Print the arrays with the maximum shape
    for array in sub_arrays:
        if array.size == max_shape:
            print('Largest sub matrix Whose value are all +ve-',array)
            print()
else:
    print('There Are No Largest sub matrix Exist Whose value are all +ve')
Largest sub matrix Whose value are all +ve- [[ 2  3  4]
 [ 6  7  8]
 [10 11 12]]

Largest sub matrix Whose value are all +ve- [[ 5  6  7]
 [ 9 10 11]
 [13 14 15]]

tags

Related  [Free] Ai Title Generator for YouTube and Blog post

numpy,array,2d array,matrix,array in python,extract sub array in python,extract sub array Using Programming,extract sub matrices Using Programming


Share

Leave a Reply

Your email address will not be published. Required fields are marked *