Splitting Matrix based on another matrix
Show older comments
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 Comments
Rounak Saha Niloy
on 19 Oct 2022
Matt J
on 19 Oct 2022
Please give the intended result for the example you've posted.
Rounak Saha Niloy
on 19 Oct 2022
Matt J
on 19 Oct 2022
WHat does it mean to be "unique in cx with respect to popx"?
Rounak Saha Niloy
on 19 Oct 2022
Accepted Answer
More Answers (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]
1 Comment
Rounak Saha Niloy
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
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 Comments
Rounak Saha Niloy
on 19 Oct 2022
Edited: Rounak Saha Niloy
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
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
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
Matt J
on 19 Oct 2022
Yes, I fixed it.
Rounak Saha Niloy
on 19 Oct 2022
Matt J
on 19 Oct 2022
Such as?
Rounak Saha Niloy
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
Matt J
on 19 Oct 2022
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
on 19 Oct 2022
Rounak Saha Niloy
on 19 Oct 2022
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!