2013-02-19 172 views
0

我有一個118800x6矩陣。第一列包含1到99的值(每個值有1200行)。現在我需要爲每個99個值創建一個新的矩陣,該矩陣包含900個隨機行(全部前一列;從原始矩陣中提取行)。我嘗試了一個for循環,但這意味着我必須編寫99行代碼......有更快的方法嗎? 預先感謝您。matlab循環中的子矩陣

+0

的以前的所有列要求有我百思不得其解,但我認爲「randperm」將幫助你。 – macduff 2013-02-20 00:15:10

+1

爲什麼一個for循環意味着你必須編寫99行代碼? – Morgan 2013-02-20 00:40:08

+0

有沒有什麼辦法可以看到99行代碼的樣子? – johnish 2013-02-20 02:21:57

回答

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