1

I have a 640x 640 matrix that I am trying to split into smaller matrices (similar to split.ppp) but diagonally, x amount of times. How would I write a code such that: it would cut the matrix diagonally, write all the values inside that diagonal chunk into new matrices?

Extracting off-diagonal slice of large matrix basically like this question but to iterate it throughout the matrix until all slices have been accounted for

I have tried using all the methods here as a base, but have been unsuccesful in applying it to my problem

https://i.stack.imgur.com/52Ml2.jpg

From the picture, I have taken a matrix, spliced it up into strips ( as in the picture) and then each splice will be plotted as point pattern. I wanto do the same, but diagonally splice it up this time

I have cut the matrix up vertically 30 times, and then off each 'piece' conducted spatial analysis on them. I am trying to do the same but now diagonally.

 #original
1 0 0 1 1 0
1 1 0 0 1 1
1 0 1 1 0 0
0 1 0 1 1 1
1 1 0 1 1 0
1 1 1 0 1 0
#new sub matrix containing slice
1 x x x x x
0 1 x x x x
1 0 1 x x x
x 0 1 1 x x
x x 1 0 0 x
x x x 1 0 1  
#next sub matrix ( moving along the diagonal
x 0 1 1 x x
x x 1 0 1 x
x x x 0 1 1
x x x x 0 0
x x x x x 1
x x x x x x 

and repeat until all values are accounted for

  • 1
    Let's say your input is `set.seed(42); m <- matrix(sample(0:1, 100, TRUE), 10)`. What would be the expected result? – Roland Aug 05 '19 at 13:05
  • Please edit the information into the question. What I provide in my comment above is a minimal reproducible example. All this image manipulation stuff doesn't seem relevant to your actual question. However, I simply don't understand how splitting a matrix diagonally would result in a matrix (except for the trivial case where each (off-)diagonal is split off). – Roland Aug 05 '19 at 13:27
  • Please edit your question. We can't see formatting in comments ... – Roland Aug 05 '19 at 13:31
  • Thanks which part must i edit? – Joel Annett Aug 05 '19 at 13:38
  • 1
    Show simple, small, reproducible example input (instead of code relying on a file as input). This should be easy because apparently you are dealing with a matrix of binary values. Then show the expected output (not through plotting, show an R object or at least a simple text representation, e.g., `print` output). – Roland Aug 05 '19 at 13:42
  • What do you mean by `x` in the expected result? `0`, `NA`, something else? – Gregor Thomas Aug 05 '19 at 13:47
  • sorry x would just be null values correct – Joel Annett Aug 05 '19 at 13:52
  • `NULL` isn't really an option for a `matrix`. Do you mean `NA`? – Gregor Thomas Aug 05 '19 at 13:53
  • Would it be OK to set the `x` to zero? Then this would be trivial with the Matrix package, e.g., for the first slice `library(Matrix); as(band(m, -2, 0), "sparseMatrix")`. – Roland Aug 05 '19 at 14:04
  • x could then be 0 or NA, whichever is easier – Joel Annett Aug 05 '19 at 14:08
  • edited matrices to be 0's an 1's , sorry typo – Joel Annett Aug 05 '19 at 14:08

1 Answers1

1

Here is an approach using the Matrix package.

#some input
set.seed(42); m <- matrix(sample(0:1, 100, TRUE), 10)

library(Matrix)

First we define a "mask" for the slice by creating a bandsparse matrix:

mask <- bandSparse(nrow(m), ncol(m), (-2):0)
#10 x 10 sparse Matrix of class "ntCMatrix"
#                         
# [1,] | . . . . . . . . .
# [2,] | | . . . . . . . .
# [3,] | | | . . . . . . .
# [4,] . | | | . . . . . .
# [5,] . . | | | . . . . .
# [6,] . . . | | | . . . .
# [7,] . . . . | | | . . .
# [8,] . . . . . | | | . .
# [9,] . . . . . . | | | .
#[10,] . . . . . . . | | |

(-2):0 defines the off-diagonals relative to the diagonal which is 0.

Then we can apply the mask:

m1 <- m
m1[!as.matrix(mask)] <- NA
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    0   NA   NA   NA   NA   NA   NA   NA   NA    NA
# [2,]    0    1   NA   NA   NA   NA   NA   NA   NA    NA
# [3,]    0    0    0   NA   NA   NA   NA   NA   NA    NA
# [4,]   NA    1    0    1   NA   NA   NA   NA   NA    NA
# [5,]   NA   NA    0    0    1   NA   NA   NA   NA    NA
# [6,]   NA   NA   NA    1    1    1   NA   NA   NA    NA
# [7,]   NA   NA   NA   NA    1    0    1   NA   NA    NA
# [8,]   NA   NA   NA   NA   NA    1    0    0   NA    NA
# [9,]   NA   NA   NA   NA   NA   NA    1    0    0    NA
#[10,]   NA   NA   NA   NA   NA   NA   NA    0    0     1

If you don't need to distinguish 0 and NA, it becomes even easier:

as.matrix(band(m, -2, 0))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    0    0    0    0    0    0    0    0    0     0
# [2,]    0    1    0    0    0    0    0    0    0     0
# [3,]    0    0    0    0    0    0    0    0    0     0
# [4,]    0    1    0    1    0    0    0    0    0     0
# [5,]    0    0    0    0    1    0    0    0    0     0
# [6,]    0    0    0    1    1    1    0    0    0     0
# [7,]    0    0    0    0    1    0    1    0    0     0
# [8,]    0    0    0    0    0    1    0    0    0     0
# [9,]    0    0    0    0    0    0    1    0    0     0
#[10,]    0    0    0    0    0    0    0    0    0     1

To get all slices, you only need to wrap this in a loop which iterates over diagonal numbers defining your slices.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • for(i in 1:20){ m1[[i]]<-as.matrix(band(m,-352+32*(i),-320+32*(i))) } would it be something like this for 20 iterations over a 640x640 matrix? – Joel Annett Aug 05 '19 at 15:25