Sample matrices with replacement (keeping original positions?)

I have three 10x10 matrices:

mat1 <- matrix(1:100, nrow=10, ncol=10)
mat2 <- matrix(101:200, nrow=10, ncol=10)
mat3 <- matrix(201:300, nrow=10, ncol=10)

Each number is unique for illustration purposes:

> mat1
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> mat2
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  101  111  121  131  141  151  161  171  181   191
 [2,]  102  112  122  132  142  152  162  172  182   192
 [3,]  103  113  123  133  143  153  163  173  183   193
 [4,]  104  114  124  134  144  154  164  174  184   194
 [5,]  105  115  125  135  145  155  165  175  185   195
 [6,]  106  116  126  136  146  156  166  176  186   196
 [7,]  107  117  127  137  147  157  167  177  187   197
 [8,]  108  118  128  138  148  158  168  178  188   198
 [9,]  109  119  129  139  149  159  169  179  189   199
[10,]  110  120  130  140  150  160  170  180  190   200
> mat3
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  201  211  221  231  241  251  261  271  281   291
 [2,]  202  212  222  232  242  252  262  272  282   292
 [3,]  203  213  223  233  243  253  263  273  283   293
 [4,]  204  214  224  234  244  254  264  274  284   294
 [5,]  205  215  225  235  245  255  265  275  285   295
 [6,]  206  216  226  236  246  256  266  276  286   296
 [7,]  207  217  227  237  247  257  267  277  287   297
 [8,]  208  218  228  238  248  258  268  278  288   298
 [9,]  209  219  229  239  249  259  269  279  289   299
[10,]  210  220  230  240  250  260  270  280  290   300

I want to use these 3 matrices to create 1 (or more) bootstrapped matrices - so sampling with replacement. However, I want to preserve the positions (indices? coordinates?) For example, I want a resulting bootstrapped matrix in position [1,1] to only have three possible values: 1, 101, or 201.

Likewise, I want position [10,10] to only have possible values of 100, 200, or 300.

How can I do this? How can I specify the number of resulting boostrapped matrices ?

I prefer examples to have fewer elements, i reduced 10x10 to 2x2

(mat1 <- matrix(1:4, nrow=2, ncol=2))
(mat2 <- matrix(5*(1:4), nrow=2, ncol=2))
(mat3 <- matrix(7*(1:4), nrow=2, ncol=2))

list_of_src_mats <- list(mat1,mat2,mat3)
#which matrix to go to for any element

make_a_matrix <- function(){
(mat_source <- sample.int(n=3,
           size = 4,replace=TRUE))

(contents <- imap_dbl(mat_source,~list_of_src_mats[[.x]][[.y]]))

(mat_result <- matrix(contents,nrow=2,ncol=2))
}
#make 1
make_a_matrix()
#make 3 
map(1:3,~make_a_matrix())
1 Like

Thank you! What does size = 4 do here in the context of matrices with larger dimensions?

If these were now 10x10, I can make your solution work but i'm a bit confused when I modify the size argument.

Size is the number of cells in the matrix. 2x2 is 4, 10x10 would be 100

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.