2011-04-12 38 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