# 寻找所有元素相等的最大平方子矩阵Finding the maximum square sub-matrix with all equal elements

## 2 个回复2

### #1楼 票数：2

``````r <- list()
for (w in rev(seq(min(dim(M))))) {
for (rs in seq(nrow(M)-w+1)) {
for (cs in seq(ncol(M)-w+1)) {
mat <- M[rs-1+(1:w),cs-1+(1:w)]
u <- unique(c(mat))
if (all(u!=0) &length(u)==1) r[[length(r)+1]] <- mat
}
}
if (length(r)>0) break
}
``````

``````> r
[[1]]
[,1] [,2]
[1,]    3    3
[2,]    3    3

[[2]]
[,1] [,2]
[1,]    2    2
[2,]    2    2

[[3]]
[,1] [,2]
[1,]    3    3
[2,]    3    3

[[4]]
[,1] [,2]
[1,]    2    2
[2,]    2    2

[[5]]
[,1] [,2]
[1,]    1    1
[2,]    1    1

[[6]]
[,1] [,2]
[1,]    1    1
[2,]    1    1

[[7]]
[,1] [,2]
[1,]    3    3
[2,]    3    3

[[8]]
[,1] [,2]
[1,]    3    3
[2,]    3    3
``````

``````M <- structure(c(1L, 3L, 1L, 2L, 1L, 3L, 3L, 2L, 2L, 3L, 3L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
2L, 2L, 1L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 1L, 3L, 2L, 1L,
1L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,
3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L,
3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 2L, 1L, 3L, 3L, 2L, 3L, 2L, 1L,
2L, 1L, 3L, 3L, 1L, 2L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 1L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 1L, 1L, 2L, 2L,
1L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 3L), .Dim = c(15L, 10L))

> M
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    2    2    1    1    3    2    2    1     3
[2,]    3    2    1    3    3    1    2    3    1     3
[3,]    1    2    3    2    3    1    2    2    2     1
[4,]    2    3    1    2    2    2    3    1    2     1
[5,]    1    1    3    3    3    1    2    2    2     2
[6,]    3    3    2    3    3    1    2    1    1     2
[7,]    3    1    2    2    2    1    3    3    1     1
[8,]    2    1    2    2    3    1    3    3    1     2
[9,]    2    1    2    2    3    3    3    1    2     3
[10,]    3    1    3    2    1    2    1    2    1     3
[11,]    3    2    2    1    1    1    2    1    3     3
[12,]    1    1    1    2    1    1    2    3    2     3
[13,]    1    1    3    2    1    3    1    2    3     3
[14,]    1    2    2    2    3    3    3    3    3     1
[15,]    2    2    1    2    2    3    3    3    2     3
``````

``````M <- unname(as.matrix(read.csv(file = "test2.csv")))
S <- matrix(0,nrow = nrow(M),ncol = ncol(M))
S[,1] <- M[,1]
for (i in 1:nrow(S)) {
for (j in 2:ncol(S)) {
if (M[i,j]==1) {
if (i==1) {
S[i,j] <- M[i,j]
} else {
S[i,j] <- min(c(S[i,j-1],S[i-1,j],S[i-1,j-1]))+1
}
}
}
}

inds <- which(S == max(S),arr.ind = TRUE)
w <- seq(max(S))-1
res <- lapply(seq(nrow(inds)), function(k) M[inds[k,"row"]-w,inds[k,"col"]-w])
``````

### #2楼 票数：0 已采纳

``````M1 <- M %>% data.frame %>% mutate(sumVar = rowSums(.)) %>%
arrange(desc(sumVar)) %>% dplyr::select(-sumVar)
M2 <- M1  %>% as.matrix %>% t %>% data.frame %>%
mutate(sumVar = rowSums(.)) %>% arrange(desc(sumVar)) %>%
dplyr::select(-sumVar) %>% as.matrix %>% t %>% data.frame %>%
arrange_all(funs(desc(.)))
i <- 1
j <- 1
while(sum(M2[1:i,1:j]) == i*j){
i <- i+1
j <- j+1
M3 <- M2[1:i-1,1:j-1]
}
``````

``````M <- structure(c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Dim = c(5L,
5L))
``````

``````> M
[,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    0    1
[2,]    1    1    1    1    1
[3,]    1    1    1    1    1
[4,]    1    1    1    1    1
[5,]    0    1    1    1    1
> M1
X1 X2 X3 X4 X5
1  1  1  1  1  1
2  1  1  1  1  1
3  1  1  1  1  1
4  1  1  1  0  1
5  0  1  1  1  1
> M2
X1 X2 X3 X4 X5
1  1  1  1  1  1
2  1  1  1  1  1
3  1  1  1  1  1
4  1  1  1  1  0
5  1  1  1  0  1
> M3
X1 X2 X3 X4
1  1  1  1  1
2  1  1  1  1
3  1  1  1  1
4  1  1  1  1
``````

2回复

1回复

1回复

1回复

1回复

1回复

2回复

2回复