Checking a matrix for duplicates in specific row, removing the respective columns

So, I try to explain this.
I have a two-row matrix of values, [x;y], f.e.
x=[1 2 2 3 4 5 6 6 7]
y=[1 2 3 4 5 6 7 7 8]
being merged into the matrix:
d= [1 2 2 3 4 5 6 6 7;
1 2 3 4 5 6 7 7 8]
I then want to check this matrix for repeats in the first row & remove the respective columns, while saving the x values of the columns being removed as a seperate vector. So Output should be something like:
d=[1 3 4 5 7;
1 3 4 5 8]
x_cut=[2 6]
It should be noted that this would have to scan for multiple repeats of different values, as shown above.
Thank you.
Have a great day & stay safe
Claudius Appel

 채택된 답변

Bruno Luong
Bruno Luong 2020년 8월 6일
편집: Bruno Luong 2020년 8월 6일
d= [1 2 2 3 4 5 6 6 7;
1 2 3 4 5 6 7 7 8]
dd = diff([nan,d(1,:),nan])==0;
remove = dd(1:end-1) | dd(2:end)
x_cut = unique(d(1,remove),'stable')
d(:,remove) = []

댓글 수: 3

This works nicely, but I'd have a short follow up question, that is admittetly not within the original question, because I didn't realise it was necessary:
If I, instead of just conserve x_cut, wanted to cut and preserve the pairs, i.e. having as output:
cut=[2 2 6 6;
2 3 7 7]
I am having trouble figuring out how excactly that would be done.
I was experimenting with extracting y_cut as is done with x_cut, but that leaves me with the problem that I don't know how to merge those into the right vectors again:
x_cut = unique(d(1,remove),'stable')
% yields
x_cut =
2 6
y_cut=unique(d(2,remove),'stable')
% would yield
y_cut =
2 3 7
I wouldn't know how to merge these together to form:
cut=[2 2 6 6;
2 3 7 7]
d= [1 2 2 3 4 5 6 6 7;
1 2 3 4 5 6 7 7 8]
dd = diff([nan,d(1,:),nan])==0;
remove = dd(1:end-1) | dd(2:end);
cut = d(:,remove)
keep = d(:,~remove) % rename keep to d if you like
Or if yoy want to get cut from x_cut
dd = diff([nan,d(1,:),nan])==0;
remove = dd(1:end-1) | dd(2:end)
x_cut = unique(d(1,remove),'stable')
cut = d(:,ismember(d(1,:), x_cut))
d(:,remove) = []
Thank you.
That works perfectly.
Have a nice day & stay healthy.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품

릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by