【问题标题】:Randperm Paired Columns of a Matrix矩阵的 Randperm 配对列
【发布时间】:2016-03-09 18:09:09
【问题描述】:

我想知道是否可以使用 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

谢谢。

【问题讨论】:

  • 我们可以看看你的方法吗?

标签: arrays matlab permutation


【解决方案1】:
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]

【讨论】:

  • 打败了我一秒钟!
  • 相同的列仍然配对在一起:)
  • @Ben 所有块的大小都相同吗?如果是这样,您只需要两次重塑。稍后我会回复你
  • @BenJHC 请立即查看
猜你喜欢
  • 2014-12-16
  • 1970-01-01
  • 2011-12-08
  • 2018-01-21
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多