I'm following this approach to find rectangles of 1's in a (0,1) matrix
Find 5 consecutive numbers >= 3 for each row of a matrix
but there's a problem when I try to find 1x4 rectangles (1 row and 4 columns) like in this matrix
m <- matrix(c(0,1,1,1,1,0,0,0,1,0,1,0,0,1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0), nrow = 6, ncol = 5, byrow = TRUE)
There are four rectangles: 1st row, 4th row, 5th row and 6th row
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 1 1 1
[2,] 0 0 0 1 0
[3,] 1 0 0 1 0
[4,] 1 1 1 1 0
[5,] 0 1 1 1 1
[6,] 1 1 1 1 0
when I run the code for this case I get the 4 results, but two of them (4th and 6th row) both results start at first column but in the result I get as starting column the 5th and ending column the 8th when the matrix is 6x5 and can't find out which is the problem.
b<-m
for (j in 1:5){
cont<-1
for (i in 6:1) {
if (m[i,j]==0){
cont<-1
b[i,j]<-NA}
else {b[i,j]<-cont
cont<-cont+1}
}
}
mdat<-b
apply(mdat, 1, function(x) {
r <- rle(x >= 1)
w <- which(!is.na(r$values) & r$values & r$lengths >=4)
if (length(w) > 0) {
before <- sum(r$lengths[1:(w[1]-1)])
c(before+1,before+ r$lengths[w[1]])
} else
NULL
})
[[1]]
[1] 2 5
[[2]]
NULL
[[3]]
NULL
[[4]]
[1] 5 8
[[5]]
[1] 2 5
[[6]]
[1] 5 8
1's in a single row? - David Arenburg