Splitting Matrix based on another matrix
이전 댓글 표시
I have two matrices as follows.
popx=[52.647 10.912 2.389
52.564 10.911 2.389
52.569 10.912 2.389
52.569 10.912 2.389
52.569 10.913 2.389
52.569 10.913 2.389];
cx=[52.646 10.912 2.389
52.564 10.911 2.389
52.569 10.913 2.403
52.570 10.912 2.389
52.569 10.913 2.389
52.569 10.912 2.389];
Now, I want to split cx into two matrirces as per the following-
- rows which are UNIQUE with respect to popx.
- rows wich are NOT UNIQUE with respect to cx.
Finally, I will again merge these two matrices which will be equivalent to cx (i do not mind if the order of rows are diffferent).
How can I do this?
댓글 수: 5
Rounak Saha Niloy
2022년 10월 19일
Matt J
2022년 10월 19일
Please give the intended result for the example you've posted.
Rounak Saha Niloy
2022년 10월 19일
Matt J
2022년 10월 19일
WHat does it mean to be "unique in cx with respect to popx"?
Rounak Saha Niloy
2022년 10월 19일
채택된 답변
추가 답변 (3개)
This might be what you want.
popx=[52.647 10.912 2.389
52.564 10.911 2.389
52.569 10.912 2.389
52.569 10.912 2.389
52.569 10.913 2.389
52.569 10.913 2.389];
cx=[52.646 10.912 2.389
52.564 10.911 2.389
52.569 10.913 2.403
52.570 10.912 2.389
52.569 10.913 2.389
52.569 10.912 2.389];
[~,~,Gp]=unique(popx,'rows');
[~,~,Gc]=unique(cx,'rows');
Hp=histcounts(Gp,1:max(Gp)+1);
Hc=histcounts(Gc,1:max(Gc)+1);
crit1=(Hp==1);
crit2=(Hc>1);
M1=cx(crit1(Gp),:)
M2=cx(crit2(Gc),:)
result=[M1,M2]
Rounak Saha Niloy
2022년 10월 19일
popx=[52.647 10.912 2.389
52.564 10.911 2.389
52.569 10.912 2.389
52.569 10.912 2.389
52.569 10.913 2.389
52.569 10.913 2.389];
cx=[52.646 10.912 2.389
52.564 10.911 2.389
52.569 10.913 2.403
52.570 10.912 2.389
52.569 10.913 2.389
52.569 10.912 2.389];
[A,ia]=setdiff(cx,popx,'rows')
B=setdiff(cx,A,'rows')
C=[A;B]
댓글 수: 16
Rounak Saha Niloy
2022년 10월 19일
편집: Rounak Saha Niloy
2022년 10월 19일
Rounak Saha Niloy
2022년 10월 19일
Rounak Saha Niloy
2022년 10월 19일
A0=setdiff(cx,popx,'rows');
B0=setdiff(cx,A0,'rows');
loc=ismember(cx,A0,'rows');
A=cx(loc,:);
loc=ismember(popx,B0,'rows');
B=popx(loc,:); %loc contains desired indices into popx
Rounak Saha Niloy
2022년 10월 19일
Rounak Saha Niloy
2022년 10월 19일
Matt J
2022년 10월 19일
Yes, I fixed it.
Rounak Saha Niloy
2022년 10월 19일
Matt J
2022년 10월 19일
Such as?
Rounak Saha Niloy
2022년 10월 19일
Rounak Saha Niloy
2022년 10월 19일
Rounak Saha Niloy
2022년 10월 19일
Matt J
2022년 10월 19일
A0=setdiff(cx,popx,'rows');
B0=setdiff(cx,A0,'rows');
loc=ismember(cx,A0,'rows');
A=cx(loc,:);
loc=ismember(cx,B0,'rows');
B=cx(loc,:); %loc contains desired indices into popx
C=[A;B];
popxIndices=ismember(popx,C,'rows')
Rounak Saha Niloy
2022년 10월 19일
Rounak Saha Niloy
2022년 10월 19일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
