Randperm配对列的矩阵

时间:2016-03-09 18:09:10

标签: arrays matlab permutation

我想知道是否可以使用randperm来重新排列由成对相同列组成的细胞矩阵。例如,

S S S S S S L L L L L L
1 1 3 3 5 5 1 1 3 3 5 5

S S L L S S S S L L L L
3 3 1 1 5 5 1 1 5 5 3 3

编辑:我的意思是更像是将配对列(或迷你块)随机置换以形成如上所示的矩阵。

S S    S S    S S    L L    L L    L L   
1 1    3 3    5 5    1 1    3 3    5 5

谢谢。

1 个答案:

答案 0 :(得分:3)

c = {'S' 'S' 'S' 'S' 'S' 'S' 'L' 'L' 'L' 'L' 'L' 'L';
      1   1   3   3   5   5   1   1   3   3   5   5}; %// data: cell array
N = 2; %// number of columns per block
d = reshape(c, 2*size(c,1), []); %// pack each group of N columns into a single column
ind = randperm(size(d,2));  %// random permutation of packed-column indices
result = d(:,ind); %// apply those indices
result = reshape(result, size(c,1), []); %// unpack columns

示例结果是

result = 
    'L'    'L'    'L'    'L'    'S'    'S'    'S'    'S'    'L'    'L'    'S'    'S'
    [1]    [1]    [5]    [5]    [5]    [5]    [3]    [3]    [3]    [3]    [1]    [1]