2

I create a matrix in R with 10x10 (10 rows and 10 columns): matriz <- matrix(1:100, nrow = 10, ncol = 10, byrow=T) I want to extract square submatrices (3x3) from matrix (matriz), randomly and without overlap.

I see a package in R named "subset.matrix", but I couldn't in randomly matrix.

Any suggestion?

  • What do you mean randomly and without overlap? How many submatrices do you wish to "extract"? If they cannot overlap it is not completely random? – user2974951 Aug 11 '21 at 13:00
  • Oh sorry! what I meant is that the sub-matrices can be taken from anywhere as long as the values don't match – Josiane Souza Aug 11 '21 at 13:50

2 Answers2

2

You can define the following function f

f <- function(mat, submat.size = 3) {
  ridx <- Filter(function(x) length(x) == submat.size, split(sample(seq(nrow(mat))), ceiling(seq(nrow(mat)) / submat.size)))
  cidx <- Filter(function(x) length(x) == submat.size, split(sample(seq(ncol(mat))), ceiling(seq(ncol(mat)) / submat.size)))
  replicate(2, mat[ridx[[sample(length(ridx), 1)]], cidx[[sample(length(cidx), 1)]]], simplify = FALSE)
}

and this function enables you to generate a pair of sub-matrices which are random and non-overlapped.

Example Result

> f(matriz)
[[1]]
     [,1] [,2] [,3]
[1,]   68   67   70
[2,]   38   37   40
[3,]   88   87   90

[[2]]
     [,1] [,2] [,3]
[1,]   63   62   69
[2,]   33   32   39
[3,]   83   82   89

If you want all possible exclusive random sub-matrices each time, you can try

f2 <- function(mat, submat.size = 3) {
  ridx <- Filter(function(x) length(x) == submat.size, split(sample(seq(nrow(mat))), ceiling(seq(nrow(mat)) / submat.size)))
  cidx <- Filter(function(x) length(x) == submat.size, split(sample(seq(ncol(mat))), ceiling(seq(ncol(mat)) / submat.size)))
  r <- list()
  for (i in seq_along(ridx)) {
    for (j in seq_along(cidx)) {
      r[[length(r) + 1]] <- mat[ridx[[i]], cidx[[j]]]
    }
  }
  r
}

and you will obtain

> f2(matriz)
[[1]]
     [,1] [,2] [,3]
[1,]    3    6    5
[2,]   63   66   65
[3,]   83   86   85

[[2]]
     [,1] [,2] [,3]
[1,]    2    8    4
[2,]   62   68   64
[3,]   82   88   84

[[3]]
     [,1] [,2] [,3]
[1,]    1   10    7
[2,]   61   70   67
[3,]   81   90   87

[[4]]
     [,1] [,2] [,3]
[1,]   13   16   15
[2,]   33   36   35
[3,]   23   26   25

[[5]]
     [,1] [,2] [,3]
[1,]   12   18   14
[2,]   32   38   34
[3,]   22   28   24

[[6]]
     [,1] [,2] [,3]
[1,]   11   20   17
[2,]   31   40   37
[3,]   21   30   27

[[7]]
     [,1] [,2] [,3]
[1,]   43   46   45
[2,]   53   56   55
[3,]   73   76   75

[[8]]
     [,1] [,2] [,3]
[1,]   42   48   44
[2,]   52   58   54
[3,]   72   78   74

[[9]]
     [,1] [,2] [,3]
[1,]   41   50   47
[2,]   51   60   57
[3,]   71   80   77
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1

I agree with the comment from user2974951 regarding randomness. However, this code block will do what you asked.

matriz <- matrix(1:100, nrow = 10, ncol = 10, byrow=T)

attempts <- 50

# Initialize a list to hold the results
sub_mats <- vector(mode = "list", length = attempts)

# The top left corner of the matrix can't have an index > 8
rand_x <- sample(1:8, attempts, replace = T)
rand_y <- sample(1:8, attempts, replace = T)

for (i in 1:attempts) {
  # Get the three-length vectors
  x_range <- rand_x[i] : (rand_x[i] + 2)
  y_range <- rand_y[i] : (rand_y[i] + 2)
  # Subset the matrix
  sub_mat <- matriz[x_range, y_range]
  # We'll use NAs to mark submatrices from previous loops
  if (any(is.na(sub_mat))) next
  # If there's no overlap, add it to the list
  sub_mats[[i]] <- sub_mat
  # Set this submatrix as NAs
  matriz[x_range, y_range] <- rep(NA, 9)
}

# Remove failed attempts
sub_mats <- sub_mats[!sapply(sub_mats, is.null)]

Instead of a set number of attempts for the loop, you could use a counter. With 50 attempts, I get 4-6 sub-matrices. 1000 gives 6-8.

zreitz
  • 26
  • 3