how to separate a matrix into 2 vectors
조회 수: 6 (최근 30일)
이전 댓글 표시
hello guys, I have a problem with separating a matrix into two vectors, if the last column is 0 or 1.
I explain myself better, I have this matrix but as we can see in the last column I have 1 or 0 right? what I want to do is separate it into two groups for example.
matrix file attached.
matrix
60 13.9713945172825 20 1
59 14.3718712753277 43 1
52 21.1752085816448 20 1
48 12.7771156138259 35 0
48 20.7771156138259 35 0
47 15.1775923718712 28 1
44 18.3790226460073 57 1
41 21.5804529201432 8 1
15 21.9928486293206 29 1
12 21.1942789034565 31 1
Result
vectorof1= [60 13.9713945172825 20 1
59 14.3718712753277 43 1
52 21.1752085816448 20 1
47 15.1775923718712 28 1
44 18.3790226460073 57 1
41 21.5804529201432 8 1
15 21.9928486293206 29 1
12 21.1942789034565 31 1];
vectorof0 = [48 12.7771156138259 35 0
48 20.7771156138259 35 0];
thank you very much in advance.
댓글 수: 0
채택된 답변
Dave B
2022년 3월 17일
편집: Dave B
2022년 3월 17일
You can do this pretty easily with some indexing. Here's an example with some random data:
m = [rand(10,2) randi(2,10,1)-1]
m0 = m(m(:,3)==0,:) % read the right hand side as: from m, take the rows where the third column of m is 0, take all columns
m1 = m(m(:,3)==1,:) % read the right hand side as: from m, take the rows where the third column of m is 1, take all columns
To understand how this works, you can look at what you're feeding in:
ind = m(:,3)==1
(importantly it's displayed as 1 and 0, but note it's a "logical array" it's really true, false, false, false, true, ...)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!