0

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

4

1 回答 1

0

假设您的矩阵被称为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
于 2013-02-20T06:20:48.170 回答