我有一个1乘4的细胞阵列,D.每个单元元素包含2×2双矩阵.我想要独立地对每个矩阵进行随机排列,这样就可以得到与D相同大小的单元阵,但是它的矩阵元素会被置换,然后是逆的,以便再次得到原始的D。
对于单个矩阵情况,我有代码,它工作得很好,如下所示:
A=rand(3,3)
p=randperm(numel(A));
A(:)=A(p)
[p1,ind]=sort(p);
A(:)=A(ind)但它不适用于细胞阵列。
发布于 2015-07-24 14:19:21
对于您来说,最简单的解决方案是使用循环:
nd = numel(D);
D_permuted{1,nd} = [];
D_ind{1,nd} = [];
for d = 1:nd)
A=D{d};
p=randperm(numel(A));
A(:)=A(p)
[~,ind]=sort(p);
D_permuted{d} = A;
D_ind{d} = ind;
end假设您的D矩阵只是一个大小相同的矩阵(例如,2×2),那么您可以使用三维双矩阵而不是单元数组来避免循环。
例如,如果您拥有这样一个D:
n = 5;
D = repmat([1,3;2,4],1,1,n)*10 %// Example data然后你就可以像这样做排列
m = 2*2; %// Here m is the product of the dimensions of each matrix you want to shuffle
[~,I] = sort(rand(m,n)); %// This is just a trick to get the equivalent of a vectorized form of randperm as unfortunately randperm only accepts scalars
idx = reshape(I,2,2,n);
D_shuffled = D(idx);https://stackoverflow.com/questions/31610724
复制相似问题