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]
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!
