how to separate a matrix into 2 vectors
11 次查看(过去 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!