【发布时间】:2019-01-28 22:24:19
【问题描述】:
我有许多不同的 6By6 矩阵。每个矩阵包含不同的值。这些值表示布局将如何划分。 每个矩阵应该有如下一致的矩形(应该有连续的矩形,颜色代表单独的一致矩形):
所以我的问题是如何成功检测那些框(矩形)。 我想要一个数组列表作为输出。每个数组都应该引用第 i 个索引、第 j 个索引和那个矩形的值。
例如,我输入了这个矩阵 [[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. 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]]
我检测矩形的试验在这段代码中:
#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
所以我编写的代码是检测矩形,但以更独立的方式。我总是有一个输出数组,它引用一个只有一行和一列作为索引的值。
【问题讨论】:
-
你在说什么三角形?并且可以详细说明您的输出与输入的关系吗?很难理解你想做什么。
-
这是一个打字错误。我的意思是矩形而不是三角形。我会纠正它。很抱歉给您带来不便
-
您能否解释一下,您的输出与示例中的输入有何关联?您的代码有效还是有错误?
-
@Merlin1896 作为输入,我有一个矩阵。该矩阵包含不同的值。我想从这个矩阵中提取行或矩形或具有一个值的部分。所以我想要一个通过矩阵提取所有具有相似值的行的脚本。例如,我想输出这些数组的列表。数组 1 = [0,4,5] 和数组 2=[1,3,20]。 array1 中的第一个元素 0 指的是这个位置(第 0 行和第 4 列)。这意味着在第 0 行,从第 0 列到第 4 列我的值是 5。这更清楚一点吗?
-
我的代码有效,但我得到的输出与我想要的输出不匹配。
标签: python matrix gridview layout