필터 지우기
필터 지우기

splitting matrix based on a value in one column

조회 수: 43 (최근 30일)
masih
masih 2017년 6월 17일
댓글: Stephen23 2021년 2월 22일
Hi All,
I have a very large matrix part of which looks like this:
7 0
10 0
16 0
19 1
26 1
29 1
3 1
5 0
69 0
36 0
78 0
as you can see, the second column is either 0 or 1. I want to split this large matrix into sub matrices such that each sub matrix represent only the parts where the second column is 0. How do we do this? Thanks.

채택된 답변

Stephen23
Stephen23 2017년 6월 17일
편집: Stephen23 2017년 6월 17일
Hard-coded indexing:
>> M = [7,0;10,0;16,0;19,1;26,1;29,1;3,1;5,0;69,0;36,0;78,0];
>> M(M(:,2)==0,:)
ans =
7 0
10 0
16 0
5 0
69 0
36 0
78 0
>> M(M(:,2)==1,:)
ans =
19 1
26 1
29 1
3 1
General solution:
>> [~,~,X] = unique(M(:,2));
>> C = accumarray(X,1:size(M,1),[],@(r){M(r,:)});
>> C{1}
ans =
7 0
10 0
16 0
36 0
69 0
78 0
5 0
>> C{2}
ans =
26 1
19 1
29 1
3 1
  댓글 수: 7
Pol Medir
Pol Medir 2021년 2월 22일
How would you do this if the sample of data was too large to input manually?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

123456
123456 2018년 7월 18일
How could I do the same thing but divide cell array by strings from one row? Let's say, that the array looks like that:
t2 1 2
t2 2 3
t1 3 4
t2 4 5
t1 5 6
t1 6 7
I would like to recieve two arrays at the end:
1 2
2 3
4 5
and
3 4
5 6
6 7
  댓글 수: 1
Stephen23
Stephen23 2018년 7월 19일
There are several solutions. One way is to input the first column into unique, and use its third output with my answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by