how to remove duplicates but keep the same order?
Posted
by Ben Fossen
on Stack Overflow
See other posts from Stack Overflow
or by Ben Fossen
Published on 2010-06-17T20:27:59Z
Indexed on
2010/06/17
22:13 UTC
Read the original article
Hit count: 396
I have a cell array in Matlab
y = { 'd' 'f' 'a' 'g' 'g' 'a' 'w' 'h'}
I use unique(y) to get rid of the duplicates but it rearranges the strings in alphabetica order
>> unique(y)
ans =
'a' 'd' 'f' 'g' 'h' 'w'
Like this I want to remove the duplicates but keep the same order. I know I could write a function do do this but was wondering if there was a simpler way using unique to remove duplicates while keeping the same order just with the duplicates removed.
I want it to return this
>> unique(y)
ans =
'd' 'f' 'a' 'g' 'w' 'h'
© Stack Overflow or respective owner