テキストファイルから読み取った2次元配列である行列があります。テキスト ファイルの各行には 3 つのエントリがあります。3 番目の列には 1 から 4 の範囲の値があります。この値に基づいて行を分離し、別の行列に入れたいと思います。それを行う方法を提案していただけますか?
1983 次
2 に答える
2
MatrixM
の場合、説明したように
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 に答える