1

I have a 4D matrix with dimensions 7x4x24x10 (Lets call it main_mat). I want to get a matrix of size 7x4x24 (rand_mat) so that each element of this (rand_mat) matrix is actually a uniformly random draw from the main matrix (main_mat). I am sorry if I have not put the question clearly, so I try to explain:

I have a stack of 24 sheets of 7x4 elements, and I have 10 such stacks. What I want is that I get a single stack of 24 sheets of 7x4 elements in such a way that every element out of resultant single stack is uniformly randomly drawn from exactly same sheet number from within 10 stacks. How can I do it without using loops?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
AWAbid
  • 75
  • 1
  • 5
  • Your question is still not 100% clear to me. Do you want to get a 7x4x24 matrix with numbers chosen from the 7x4x24x10 matrix so that every place in the 7x4x24 matrix could only be one of the x10 in the other matrix? – Pieter12345 Jun 20 '15 at 23:20
  • Thats right. For example element (1,1,1) can only be among 10 elements available at this position i,e. they can only be from (1,1,1,randi(10)) and so on. – AWAbid Jun 20 '15 at 23:28
  • I tried something like this ' k = datasample(j,1,4);' and the results were that it randomly starts from one of the 7x4 sheet, and then all remaining 23 sheets are in successive order. I don't know if I am doing something wrong here. – AWAbid Jun 20 '15 at 23:37
  • @AWAbid - OK, I think I know what you want. For each position in this 7 x 4 x 24 matrix, you want to make sure that we uniformly select from one out of the 10 stacks? For example, given position `(1,1,1)` in the output, we would randomly choose from a stack such that `(1,1,1,randi(10))`? – rayryeng Jun 20 '15 at 23:39
  • Thats correct @rayryeng Thank you for understanding. – AWAbid Jun 20 '15 at 23:41
  • Kudos...I am waiting :) – AWAbid Jun 20 '15 at 23:43

2 Answers2

4

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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
0

This problem might not be solvable without the use of loops. One way that could work is:

mainMatrix = ... (7x4x24x10 matrix)
randMatrix = zeros(mainMatrix(:,1,1,1), mainMatrix(1,:,1,1), mainMatrix(1,1,:,1))
for x = 1:length(mainMatrix(:,1,1,1))
  for y = 1:length(mainMatrix(1,:,1,1))
    for z = 1:length(mainMatrix(1,2,:,1))
      randMatrix(x,y,z) = mainMatrix(x,y,z,randi(10))
    end
  end
end
Pieter12345
  • 1,713
  • 1
  • 11
  • 18
  • It is most certainly solvable without loops. See my answer. However, I will admit that it's a bit memory intensive. This would work if the matrix is very large an this has very minimal memory footprint. – rayryeng Jun 20 '15 at 23:59