2011-04-12 34 views
4

一個矩陣由值I不知道是否有一個MATLAB溶液到一個矩陣分裂爲子矩陣如下所示:拆分在MATLAB

的矩陣是:

A = 
16  2 3 
5  11 10 
9  7 6 
4  14 15 
5  1 3 

我想坐以5開頭到另一個矩陣的行,以16開頭到另一個矩陣的行等等。

有沒有這個功能,或者我應該去if/for approach?

+0

Matlab的應儘可能使用矢量操作進行編程,但是我沒有看到'for'和'if'的情況,這種情況 – Phonon 2011-04-12 20:20:36

回答

3

下面是一個使用的功能SORTROWSUNIQUEACCUMARRAY,和MAT2CELL創建具有每個單元存儲一組行與在第一列中的值相同的單元陣列的一個解決方案:

>> sortedA = sortrows(A,1); %# Sort the rows by the first column 
>> [~,~,uniqueIndex] = unique(sortedA(:,1)); %# Find indices of unique values 
               %# in the first column 
>> cellA = mat2cell(sortedA,...      %# Break matrix up by rows 
        accumarray(uniqueIndex(:),1),3); %# into a cell array 
>> cellA{:} %# Display the contents of the cells 

ans = 

    4 14 15 

ans = 

    5 11 10 
    5  1  3 

ans = 

    9  7  6 

ans = 

    16  2  3 
+0

謝謝,這工作像一個魅力。 – Ramala 2011-04-12 20:47:33

1

我想我發現它=)

for n=1:max(max(A)) 
M{n} = A(find(A(:,1)==n),:); 
end 

現在M{n}是與n開始所有行的矩陣。 =)

+0

真棒,謝謝! – Ramala 2011-04-12 20:47:09