微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在给定矩阵中找到所有子矩阵值都大于阈值不使用 numpy 和 Scipy

如何解决如何在给定矩阵中找到所有子矩阵值都大于阈值不使用 numpy 和 Scipy

我正在研究不支持 numpy 或 scipy 的微控制器。我想提取矩阵中大于给定阈值的所有子矩阵。

myMtarix = [[0,0],[0,204,60,284,100,74,421,157,317,364,736,245,1470,717,258,879,496,671,766,725,1429,0]]

myMatrix = 6*16 矩阵,我想提取

subMatrix1 = [[100],[717]]

subMatrix2 = [[0,[74,157],[258,496]]

subMatrix3 = [[0,284],[317,1470],1429]]

和阈值 = 10

我试过这样的事情

这里,收集值>阈值及其索引

    threshold = 10
    pressureIndexes = []
    pressurePoints = []
    reqValue = []
    reqIndex = []
    
    reqValue = [myMatrix[i][j] for i in range(numRows) for j in range(numCols) if(myMatrix[i][j] > threshold)]
    reqIndex = [[i,j] for i in range(numRows) for j in range(numCols) if(myMatrix[i][j] > threshold)]
 

从这里开始,我尝试提取子矩阵的确切边界

    Xend = Xstart = reqIndex[0][0]
    Yend = Ystart = reqIndex[0][1]
    dummy = []  
    for i in range(1,len(reqIndex)):
        PXstart = Xstart
        PXend  = Xend
        PYstart = Ystart
        PYend  = Yend
        Xstart = min(Xstart,reqIndex[i][0])
        Xend = max(Xend,reqIndex[i][0])
        Ystart = min(PYstart,reqIndex[i][1])
        Yend = max(PYend,reqIndex[i][1])
        if(abs(PXend-Xend) > 2 or abs(PYend-Yend) > 2):
            dummy.append([[PXstart,PXend],[PYstart,PYend]])
            Xend = Xstart = reqIndex[i][0]
            Yend = Ystart = reqIndex[i][1]
    dummy.append([[Xstart,Xend],[Ystart,Yend]])
    print()
    
    for i in dummy:
        print(i)
    print('---------------------------------------------------------')
    sleep(1)

输出

[0,0]
[0,0]
        
                
[[1,1],[4,4]]  # [[rowStarting,rowEnding],[colStarting,colEnding]] for value 204
[[1,[11,11]]
[[1,3],[1,14]]

解决方法

这是一种方法,它将返回一个没有 0 行或列的子矩阵列表:

mm       = [[0,0],[0,204,60,284,100,74,421,157,317,364,736,245,1470,717,258,879,496,671,766,725,1429,0]]




## values below threshold are set to 0
def threshold(m,th=10):
    for l in range(len(m)):
        for c in range(len(m[l])):
            v=m[l][c]
            if v < th:
                v = 0
            m[l][c] = v
    return m


## helper functions to handle matrixes

## work with a colum values
def column(m,c):
    return list(map(lambda x: x[c],m))


## cut and drop left of matrix
def cutleftofcolumn(m,c):
    return list(map(lambda x: x[c+1:],m))


## cut and drop right of matrix
def cutrightofcolumn(m,c):
    return list(map(lambda x: x[:c],m))


## remove 0 borders of the matrix
def removeBorders(m: list) -> list:
    test = True
    while test:
        test = False
        #if sum(m[0])==0:
        if all(map(lambda x: x==0,m[0])):
            m = m[1:]
            test = True
        #if sum(m[-1])==0:
        if all(map(lambda x: x==0,m[-1])):
            m = m[:-1]
            test = True
        #if sum(column(m,0))==0:
        if all(map(lambda x: x==0,column(m,0))):
            m=cutleftofcolumn(m,0)
            test = True
        #if sum(column(m,-1))==0:
        if all(map(lambda x: x==0,-1))):
            m=cutrightofcolumn(m,-1)
            test = True
    return m


## split on a 0 column and returns 2 matrices
def splitv(m,c):
    a=cutleftofcolumn(m,c)
    b=cutrightofcolumn(m,c)
    return [a,b]


## split on a 0 line and returns 2 matrices
def splith(m,l):
    return [m[0:l],m[l+1:]]



mm = threshold(mm,10)

listMatrices = [mm]


test = True
while (test):
    test=False
    index = 0
    while (len (listMatrices)>index):

        ## get first matrix of the list
        m = listMatrices.pop(0)

        ## removes 0 borders of the matrix
        m = removeBorders(m)
    
        lll=[]    
        ## try to find an horizontal split on 0 line
        if lll==[]:
            for l in range(len(m)):
                #if sum(m[l])==0:
                if all(map(lambda x: x==0,m[l])):
                    lll = lll + splith(m,l)
                    test = True
                    break
    
        ## try to find a vertical split on 0 column
        if lll==[]:
            for c in range(len(m[0])):
                cc = column(m,c)
                #if sum(cc)==0:
                if all(map(lambda x: x==0,cc)):
                    lll = lll + splitv(m,c)
                    #print("Csum=0",c)
                    test = True
                    break
            
        if lll==[]:
            listMatrices.append(m)
        else:
            for l in lll:
                listMatrices.append(l)
        index+=1


print(listMatrices)

输出为:

[[[0,284],[317,1470],1429]],[[0,[74,157],[258,496]],[[100],[717]]]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。