Python 如何检测矩阵中的长方体
我有很多不同的6By6矩阵。每个矩阵包含不同的值。这些值表示布局的划分方式。 每个矩阵应具有如下一致的矩形(应具有连续的矩形,颜色表示单独的一致矩形): 所以我的问题是,如何成功地检测这些长方体。 我想要一个数组列表作为输出。每个数组都应该引用第i个索引、第j个索引和该矩形的值 例如,我将此矩阵[[35.11.11.11.11.0.]、[10.10.10.10.0.]、[10.10.10.10.0.]作为输入[ 34.34.34.34.34.0.],[34.34.34.34.34.0.],[0.0.0.0.] 所以我想要作为输出[[0,0,35],[0,4,11],[1,4,10],[2,4,10],[3,4,34],[4,4,34],[0,0,0],[1,0,0],[5,5,0]] 我检测矩形的尝试如下代码所示:Python 如何检测矩阵中的长方体,python,matrix,gridview,layout,Python,Matrix,Gridview,Layout,我有很多不同的6By6矩阵。每个矩阵包含不同的值。这些值表示布局的划分方式。 每个矩阵应具有如下一致的矩形(应具有连续的矩形,颜色表示单独的一致矩形): 所以我的问题是,如何成功地检测这些长方体。 我想要一个数组列表作为输出。每个数组都应该引用第i个索引、第j个索引和该矩形的值 例如,我将此矩阵[[35.11.11.11.11.0.]、[10.10.10.10.0.]、[10.10.10.10.0.]作为输入[ 34.34.34.34.34.0.],[34.34.34.34.34.0.],[0
#Detect the rectangles in the matrices
def detect_rectangle(T):
i = 0
j = 0
elem = T[0,0]
rectanglesList = []
n,m = T.shape
while (i < n) and (j<m):
#print('i,j, elem',i,j,elem)
if (i == n-1 and j == m-1): # if we reached the end of the matrix
rectanglesList.append([i,j,elem])
break;
if (j == m-1): #in case we reached the end of columns, we reeinitialize the columns
if (i != n -1):
i += 1
elem = T[i,j]
else:
rectanglesList.append([i,j,T[i,j]])
j = 0
break;
elif T[i,j] == T[i,j+1]: #in case the element in the next column is equal, continue and check further, store it as elem
j +=1
elem = T[i,j]
elif T[i,j] != T[i,j+1] :
rectanglesList.append([i,j,T[i,j]])
j += 1
elem = T[i,j]
if (i == n-1): #in case we reached the end of rows
if j != n -1 :
j += 1
elem = T[i,j]
else:
rectanglesList.append([i,j,elem])
i = 0
break
else:
if (T[i,j] == T[i+1,j]) and (elem == T[i,j]): #in case the element in the next row is equal
i += 1
elif (T[i,j] == T[i+1,j]) and (elem != T[i,j]): #in case the element in the next row is equal
elem = T[i,j]
i+= 1
elif ((T[i,j] != T[i+1,j] and elem == T[i,j])): #in case it is not equal to neither the element in the next row nor the element in the next column
rectanglesList.append([i,j,elem])
#j +=1
elem = T[i,j]
elif T[i,j] != T[i+1,j] :
i += 1
elem = T[i,j]
return rectanglesList
#检测矩阵中的矩形
def检测_矩形(T):
i=0
j=0
elem=T[0,0]
矩形列表=[]
n、 m=T形
而(i
x=np.array([[35,11,11,11,11,0],[10,10,10,10,10,0],
[10,10,10,10,10,0],[34,34,34,34,34,0],
[34,34,34,34,34,0], [0,0,12,12,12,0]])
outputs=[]
for i, row in enumerate(x):
last_ele=row[0]
for j, val in enumerate(row[1:]):
if val == last_ele:
continue
outputs.append([i,j, last_ele])
last_ele=val
outputs.append([i,len(row)-1, last_ele])
print(outputs)
# [[0, 0, 35], [0, 4, 11], [0, 5, 0], [1, 4, 10],
# [1, 5, 0], [2, 4, 10], [2, 5, 0], [3, 4, 34],
# [3, 5, 0], [4, 4, 34], [4, 5, 0], [5, 1, 0],
# [5, 4, 12], [5, 5, 0]]
我们只需在行上迭代一次,然后检查前面的元素是否与当前元素相同。如果不相同,则将最后看到的元素连同行和列索引一起添加到输出列表中。您在谈论什么三角形?并且可以详细说明您的输出与输入之间的关系?很难理解是什么你想这样做。这是一个打字错误。我是说矩形而不是三角形。我会纠正它。对于不方便,你能解释一下,在你的例子中,你的输出是如何与你的输入相关的吗?你的代码工作吗?还是有错误?@Merlin1896所以作为输入,我有一个矩阵。这个矩阵包含不同的值。我想从这个m中提取对行、矩形或具有一个值的部分进行矩阵运算。因此,我需要一个脚本,通过矩阵提取具有类似值的所有行。例如,我希望输出此类数组的列表。array1=[0,4,5]和array2=[1,3,20]。array1中的第一个元素0表示此位置(第0行和第4列)。这意味着在第0行中,从第0列到第4列,我的值为5。这更清楚一点吗?我的代码可以工作,但我得到的输出与我想要的输出不匹配。脱帽致敬!谢谢:)