I'm working on microcontrollers, where it doesn't supports numpy or scipy. I want to extract all submatrices greater than given threshold value in matrix.
myMtarix = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 204, 0, 0, 0, 0, 0, 0, 60, 0, 0, 284, 0],
[0, 100, 0, 74, 421, 157, 0, 0, 0, 0, 317, 364, 736, 245, 1470, 0],
[0, 717, 0, 258, 879, 496, 0, 0, 0, 0, 0, 671, 766, 725, 1429, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
myMatrix = 6*16 matrix and i want to extract
subMatrix1 = [[100],
[717]]
subMatrix2 = [[0, 204, 0],
[74, 421, 157],
[258, 879, 496]]
subMatrix3 = [[0, 60, 0, 0, 284],
[317, 364, 736, 245, 1470],
[0,671, 766, 725, 1429]]
and threshold = 10
I tried something like this
Here, collecting the values > threshold and their indexes
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)]
from here, I'm try to extract the the exact boundaries of submatrices
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)
OUTPUT :
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 204, 0, 0, 0, 0, 0, 0, 60, 0, 0, 284, 0]
[0, 100, 0, 74, 421, 157, 0, 0, 0, 0, 317, 364, 736, 245, 1470, 0]
[0, 717, 0, 258, 879, 496, 0, 0, 0, 0, 0, 671, 766, 725, 1429, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[1, 1], [4, 4]] # [[rowStarting,rowEnding],[colStarting,colEnding]] for value 204
[[1, 1], [11, 11]]
[[1, 3], [1, 14]]
Here is a way to do it, it will returns a list of sub-matrices with no 0 line or column:
Output is: