I had good luck finding a way to achieve intersect() faster on stack overflow with a pre-sorted 1d vector, so I am hoping for the same luck for unique() ;)
Almost 1/4'th of my runtime is spent using unique(). I would like to speed this up, and I can assume it to be 1d pre-sorted vector. Is there any other low-level functions I can use directly to speed this up?
View MATLAB Command. Find the unique elements in a vector and then use accumarray to count the number of times each unique element appears. Create a vector of random integers from 1 through 5. a = randi ( [1 5],200,1); Find the unique elements in the vector. Return the index vectors ia and ic. [C,ia,ic] = unique (a);
Furthermore the simpliest way to get unique value is to sort the vector. So the stable option should be a little bit longer to execute. Sign in to comment. Sign in to answer this question.
In my opinion unique should be "stable" by default. If someone wants to sort, than there is sort commant that for. What does sorting by default in unique has to do with removing duplicate values?
You can simply use diff
to check whether consecutive elements are the same.
vector = [1 2 3 4 4 5];
uniqueVector = vector([true;diff(vector(:))>0])
uniqueVector =
1 2 3 4 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With