If I am interpreting what you want correctly, for each unique 3D position in this matrix of 7 x 4 x 24, you want to be sure that we randomly sample from one out of the 10 stacks that share the same 3D spatial position.
What I would recommend you do is generate random integers that are from 1 to 10 that is of size 7 x 4 x 24 long, then use sub2ind
along with ndgrid
. You can certainly use randi
as you have alluded to in the comments.
We'd use ndgrid
to generate a grid of 3D coordinates, then use the random integers we generated to access the fourth dimension. Given the fact that your 4D matrix is stored in A
, do something like this:
rnd = randi(size(A,4), size(A,1), size(A,2), size(A,3));
[R,C,D] = ndgrid(1:size(A,1), 1:size(A,2), 1:size(A,3));
ind = sub2ind(size(A), R, C, D, rnd);
B = A(ind);
Bear in mind that the above code will work for any 4D matrix. The first line of code generates a 7 x 4 x 24 matrix of random integers between [1,10]
. Next, we generate a 3D grid of spatial coordinates and then use sub2ind
to generate column-major indices where we can sample from the matrix A
in such a way where each unique 3D spatial location of the matrix A
only samples from one chunk and only one chunk. We then use these column-major indices to sample from A
to produce our output matrix B
.