2012-09-09 3 views
-1

나는 텍스트 파일에서 읽은 2 차원 배열 인 행렬을 가지고있다. 텍스트 파일의 각 행에는 3 개의 항목이 있습니다. 세 번째 열은 1에서 4까지의 값을가집니다.이 값을 기준으로 행을 분리하여 다른 행렬에 넣으 려합니다. 할 수있는 방법을 제안 해 주시겠습니까?matlab에서 열의 값을 기반으로 행을 분리하는 방법은 무엇입니까?

+0

'은 1 내지 4'범위이다. 값은 실수 또는 정수입니까? – angainor

답변

2

,이 작동하는 이유를 다음과 같은 부분에서 결과를보고, 확인하려면

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` 
0

sortrows 기능을 사용하십시오.

의사 코드 : 매트릭스 M를 들어

%% 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); 
관련 문제