Cluster a 25 x 25 matrix into submatrices (3x3)

15 Views Asked by At

I need to cluster a 25x25 matrix into submatrices of 3x3. I want to start the submatrices at the upper left corner at 1,1. I know that the number is not even (625 / 9 = 69.4444) so I want to just ignore the last row (indices of 25, 50, 75, 100, 125, etc) and cluster just columns 1:24 (24/3 = 8).

Ultimately, I need the indices of each of the 9 points in the subgrids to apply to another matrix and extract the values.

What I am working with so far:

data<-(1:625)
mdat <- matrix(data, nrow = 25, ncol = 25, byrow = TRUE)

Indices of first cluster:

cluster1 <- mdat[1:3, 1:3]

Where to go from here? And how to loop through but ignore that last column? Ultimate goal is to get a sum of all the values at the indices within each cluster and save that output.

Thanks!

for (i in seq(1, 69, by = 3)) {
  cluster <- mdat[i:(i+2), i:(i+2)]
  indices <- REALDATA[cbind(cluster)]
  clusteredsum <- sum(indices)
  print(clusteredsum)
}
0

There are 0 best solutions below