A faster way to achieve unique () in matlab if 1d of a pre-sorted vector is assumed?

I was fortunate enough to find a way to execute intersect () faster when the stack overflows with a pre-sorted 1d vector, so I hope for the same luck unique ();)

Almost 1 / 4'th of my runtime is wasted using unique (). I would like to speed this up, and I can assume that this is a 1d pre-sorted vector. Are there any other low level functions that I can use to speed this up?

+6
source share
1 answer

You can simply use diff to check if consecutive elements are the same.

 vector = [1 2 3 4 4 5]; uniqueVector = vector([true;diff(vector(:))>0]) uniqueVector = 1 2 3 4 5 
+7
source

Source: https://habr.com/ru/post/901754/


All Articles