-1

我有一个矩阵,它是从文本文件中读取的二维数组。每行文本文件有 3 个条目。第 3 列的值范围为 1 到 4。我想根据该值分隔行并将它们放入不同的矩阵中。你能建议一种方法吗?

4

2 回答 2

2

M正如您所描述的,对于 Matrix

rowsContainingOne   = M( M(:,3)==1, :)
rowsContainingTwo   = M( M(:,3)==2, :)
rowsContainingThree = M( M(:,3)==3, :)
rowsContainingFour  = M( M(:,3)==4, :)

要了解其工作原理,请查看以下部分的结果:

M(:,3)              %A vector of column three
M(:,3)==1           %A logical array, `true` where column 3 equals one
M( M(:,3)==1, :)    %All columns (indicated by `:`) from rows where the logical array is `true`
于 2012-09-09T14:13:11.683 回答
0

使用功能sortrows

伪代码:

%% Creating a matrix of the type you have mentioned.
A = zeros(10,3);   A(:,1:2) = rand(10,2);    A(:,3)=randi(4,10,1);
%% Use the "sortrows" function to sort all the rows as per the entries in column-3 of A    
B = sortrows(A,3);
于 2012-09-09T07:46:06.083 回答