2014-11-06 60 views

回答

2

您可以使用arrayfun + unique組合來獲取 -

[~,~,labels] = unique(L(:,2),'stable') 
idx = arrayfun(@(x) L(labels==x,:),1:max(labels),'Uniform',0) 

顯示輸出 -

>> celldisp(idx) 
idx{1} = 
    1  2 
    3  2 
idx{2} = 
    4  6 
idx{3} = 
    5  3 
    7  3 
    1  3 
idx{4} = 
    2  7 
    9  7 
+1

哦,順便說一下,我不認爲arrayfun是一個矢量/無環的解決方案!無論如何,如果你的輸出不規則,你不能有一個矢量化的解決方案。 – Divakar 2014-11-06 11:24:54

0

您可以直接使用accumarray,也可以使用排序的數組,具體取決於您希望行的順序是否穩定,或者使submatricxes的順序穩定。

說你要對行穩定:

>> [L2s,inds] = sort(L(:,2)); 
>> M = accumarray(L2s,inds,[],@(v){L(v,:)}); 
>> M(cellfun(@isempty,M)) = []; % remove empty cells 
>> celldisp(M) 
M{1} = 
    1  2 
    3  2 
M{2} = 
    5  3 
    7  3 
    1  3 
M{3} = 
    4  6 
M{4} = 
    2  7 
    9  7