how to separate a matrix into 2 vectors
13 ビュー (過去 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 で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!