Splitting Matrix based on another matrix
Ältere Kommentare anzeigen
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 Kommentare
Rounak Saha Niloy
am 19 Okt. 2022
Matt J
am 19 Okt. 2022
Please give the intended result for the example you've posted.
Rounak Saha Niloy
am 19 Okt. 2022
Matt J
am 19 Okt. 2022
WHat does it mean to be "unique in cx with respect to popx"?
Rounak Saha Niloy
am 19 Okt. 2022
Akzeptierte Antwort
Weitere Antworten (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 Kommentar
Rounak Saha Niloy
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 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 Kommentare
Rounak Saha Niloy
am 19 Okt. 2022
Bearbeitet: Rounak Saha Niloy
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 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
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 2022
Matt J
am 19 Okt. 2022
Yes, I fixed it.
Rounak Saha Niloy
am 19 Okt. 2022
Matt J
am 19 Okt. 2022
Such as?
Rounak Saha Niloy
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 2022
Matt J
am 19 Okt. 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
am 19 Okt. 2022
Rounak Saha Niloy
am 19 Okt. 2022
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
