代码之家  ›  专栏  ›  技术社区  ›  user2089135

matlab循环子矩阵

  •  0
  • user2089135  · 技术社区  · 13 年前

    我有一个118800x6的矩阵。第一列包含从1到99的值(每个值有1200行)。现在,我需要创建一个新的矩阵,它包含900个随机行(前一列的所有行;这些行是从原始矩阵中提取的),用于每99个值。我尝试使用for循环,但这意味着我必须编写99行代码。。。有更快的方法吗? 提前谢谢。

    1 回复  |  直到 13 年前
        1
  •  0
  •   Dan fatihk    13 年前

    假设您的矩阵被调用 M :

    for num = 1:99
        %Extract only rows that have the correct number in column one
        subsample = M(M(1, :) == num, :);
        %Randomly change the order of the rows of this subsample
        shuffledsubsample = subsample(randperm(1200), :);
        %Only keep the first 900 rows
        final(:,:,num) = shuffledsubsample(1:900, :);
    end