Detection of similar neighboring values ​​in the R-matrix

I have a 10x10 matrix with 0s and 1s, and 1s are usually grouped together. I am trying to extract clusters from 1s into a list of my own matrices. Explain: this is my starting matrix:

field <- matrix(0,10,10)
field[3:4,3:4]<-1
field[6:7,7]<-1
field[7:8,8]<-1
field[8,6]<-1
field
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    0    0    0    0    0    0     0
[2,]    0    0    0    0    0    0    0    0    0     0
[3,]    0    0    1    1    0    0    0    0    0     0
[4,]    0    0    1    1    0    0    0    0    0     0
[5,]    0    0    0    0    0    0    0    0    0     0
[6,]    0    0    0    0    0    0    1    0    0     0
[7,]    0    0    0    0    0    0    1    1    0     0
[8,]    0    0    0    0    0    1    0    1    0     0
[9,]    0    0    0    0    0    0    0    0    0     0
[10,]   0    0    0    0    0    0    0    0    0     0

I would like to get a list of matrices similar to the one obtained from the following list (with border 0s to create a rectangle):

list(
    field[2:5,2:5],
    field[5:9,5:9]
  )

[[1]]
[,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    1    1    0
[3,]    0    1    1    0
[4,]    0    0    0    0

[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    1    0    0
[3,]    0    0    1    1    0
[4,]    0    1    0    1    0
[5,]    0    0    0    0    0

I don’t even know how to do this conceptually. Is there any package that exists for the relevant purpose, or any help / explanation that anyone can provide for this? Or is it simply impossible? Thanks for the help!

+4
source share
1 answer

, . Math SE .

-

,

# Generate the matrix
field <- matrix(0,10,10)
field[3:4,3:4]<-1
field[6:7,7]<-1
field[7:8,8]<-1
field[8,6]<-1

# Our similarity function, adapt as needed    
simil <- function(m1, m2)
  {
  # Check dimensions are the same
  if (any(dim(m1) != dim(m2)))
    stop(paste("ERROR: matrices are not the same size: ",
               nrow(m1), "x", ncol(m1), "vs", 
               nrow(m2), "x", ncol(m2)))

  # Linearize the matrices
  m1 <- as.vector(m1)
  m2 <- as.vector(m2)

  # Cosine similarity
  similarity <- (m1%*%m2)/sqrt((m1%*%m1) * (m2%*%m2))

  return(similarity)
  }

, , ,

m1 <- field[2:5, 2:5]
m2 <- field[6:9, 6:9]
m3 <- field[4:7, 7:10]

> simil(m1, m2)
          [,1]
[1,] 0.6708204

> simil(m1, m3)
     [,1]
[1,]    0

> simil(m2, m3)
          [,1]
[1,] 0.2581989

, :

> simil(m1,m1)
     [,1]
[1,]    1
> simil(m1,!m1)
     [,1]
[1,]    0

, for, , .

field.len <- 4

subfields <- list()
i <- 1
for (col in (1:(ncol(field)-field.len+1)))
  {
  for (row in (1:(nrow(field)-field.len+1)))
      {
      submatrix <- field[row:(row+field.len-1),col:(col+field.len-1)] 

      # Discard zero matrices
      if (sum(submatrix) > 0)
        {
        subfields[[i]] <- submatrix
        i <- i+1
        }
      }
  }

, ,

simil.matrix <- sapply(subfields, function(sf1)
  {
  res <- sapply(subfields, function(sf2)
    {
    res <- simil(sf1, sf2)

    res
    })

  res
  })

:

> simil.matrix[1,24]
[1] 0.8660254
> subfields[[1]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    1    1
[4,]    0    0    1    1
> subfields[[24]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    1    0
[4,]    0    0    1    1

> simil.matrix[10,5]
[1] 0.25
> subfields[[10]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    1    1    0    0
[3,]    1    1    0    0
[4,]    0    0    0    0
> subfields[[5]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    1    1    0
[4,]    0    1    1    0

> simil.matrix[4,5]
[1] 0
> subfields[[4]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    1    1
[2,]    0    0    0    0
[3,]    0    0    0    0
[4,]    0    0    0    0
> subfields[[5]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    1    1    0
[4,]    0    1    1    0

, , , , .

+1

Source: https://habr.com/ru/post/1525369/


All Articles