How do you remove duplicates without using unique?

I have a problem where I have to remove duplicates from a column without using unique and without sorting the data. My current code is able to remove duplicates without unique, but the data has to be sorted for it to work. Is there a way to remove the duplicates without sorting and without using unique?
z = length(A);
for i = 1:(z-1)
if A(i+1) == A(i)
A(i) = A(i) - A(i+1);
end
end
A(A==0) = [ ];
end

 채택된 답변

Star Strider
Star Strider 2016년 4월 9일
One approach:
A = [1;2;3;3;4;5;6;7;7;8;9];
Ac = zeros(size(A));
for k1 = 1:length(A)-1
Ac(k1) = sum(A(k1) == A(k1+1:end));
end
Out = A(Ac == 0)
Out =
1
2
3
4
5
6
7
8
9

댓글 수: 2

Works perfectly, thank you!
My pleasure!
It was fun to write that!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2016년 4월 9일

댓글:

2016년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by