MATLAB: How to remove duplicates in an array

arrayhomework

Ex: In the array x=[1 2 2 3 3 3 4 5], i want to eliminate all repeating elements (2,3) and retain non-repeating elements (1,4,5)
i.e., If x=[1 2 2 3 3 3 4 5], answer should be x=[1 4 5]

Best Answer

>> x=[1 2 2 3 3 3 4 5];
>> y = zeros(size(x));
>> for i = 1:length(x)
y(i) = sum(x==x(i));
end
>> for i=length(y):-1:1
if(y(i)>=2)
x(i)=[];
end
end
And your output will be
>>x
1 4 5