2012-09-04 112 views

回答

1

嘗試

B = A(randperm(length(A))) 

請查閱說明文檔。

0

你想使用randperm函數生成列索引的隨機排列(從1到列數在A),您可以再指數A有:

B = A(:, randperm(size(A, 2))); 
+0

只需要20秒就能擊敗我! – Wolfie

0

現在,代碼格式正確,很明顯OP要保留列,但randperm仍然是HPM答案中最簡單的選項。

idx = randperm(size(A,2)); % Create list of integers 1:n, in random order, 
          % where n = num of columns 

B = A(:, idx);    % Shuffles columns about, on all rows, to indixes in idx 
+0

格式化的好處。你怎麼找到的? – gnovice

+0

一些「相關」的鏈接很深,因此舊的帖子,和一個糾正無格式的代碼癢!直到我去編輯它才意識到它實際上是一個2D矩陣。 – Wolfie