简体   繁体   English

在Matlab中随机选择4D矩阵的元素

[英]Randomly select Elements of 4D matrix in Matlab

I have a 4D matrix with dimensions 7x4x24x10 (Lets call it main_mat). 我有一个尺寸为7x4x24x10的4D矩阵(我们称其为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). 我想获得一个大小为7x4x24(rand_mat)的矩阵,以便该(rand_mat)矩阵的每个元素实际上都是从主矩阵(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. 我有24张7x4元素的纸叠,我有10个这样的纸叠。 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. 我想要的是,我得到一叠24张7x4元素的纸堆,这样,从得到的单纸堆中的每个元素都是从10个纸堆中的完全相同的纸页号中均匀随机地绘制的。 How can I do it without using loops? 不使用循环怎么办?

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. 如果我正确地解释了您想要的内容,那么对于此7 x 4 x 24矩阵中的每个唯一3D位置,您需要确保我们从共享相同3D空间位置的10个堆栈中随机抽取一个。

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 . 我建议您做的是生成从1到10的随机整数,其大小为7 x 4 x 24长,然后将sub2indndgrid一起使用。 You can certainly use randi as you have alluded to in the comments. 您当然可以使用在评论中提到的randi

We'd use ndgrid to generate a grid of 3D coordinates, then use the random integers we generated to access the fourth dimension. 我们将使用ndgrid生成3D坐标的网格,然后使用生成的随机整数访问第四维。 Given the fact that your 4D matrix is stored in A , do something like this: 鉴于您的4D矩阵存储在A ,请执行以下操作:

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. 请记住,以上代码适用于任何 4D矩阵。 The first line of code generates a 7 x 4 x 24 matrix of random integers between [1,10] . 代码的第一行生成一个在[1,10]之间的随机整数的7 x 4 x 24矩阵。 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. 接下来,我们生成空间坐标的三维网格,然后使用sub2ind以产生列优先索引,我们可以从矩阵采样A以这样的方式,其中矩阵的每个独特的三维空间位置A从一个块和仅一个唯一的样本块。 We then use these column-major indices to sample from A to produce our output matrix B . 然后,我们使用这些主要列索引从A采样以生成输出矩阵B

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM