I am trying to randomly rearrange patches in a matrix. This will need to be done for larger matrices and small patches, so a for loop does not seem to be an option to achieve this. Let's say I have a data matrix like this:
data <- matrix(1:16, nrow = 4)
The output looks like
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
I now want to select 2x2 patches and rearrange them randomly, so that the output may look like this
[,1] [,2] [,3] [,4]
[1,] 11 15 3 7
[2,] 12 16 4 8
[3,] 9 13 1 5
[4,] 10 14 2 6
I achieved this so far by creating a matrix containing numbers corresponding to the index in correct order, and rearranged, but the reassignment to a new, empty matrix in a for loop for each patch gets quite time consuming when there are tens of thousands of patches.