option faster than unique?

23 views (last 30 days)
Arti Siddhpura
Arti Siddhpura on 6 Jun 2014
Edited: Sean de Wolski on 20 Apr 2015
I am working on a 955*1 double vector xy where I need to generate it 101 times and once generated, each time I need to remove repeating elements from it, sort in ascending order and get index of remaining elements. I am using unique for this. (i.e.[xyuni, ind] = unique( xy );) This operation takes almost 2 sec. I need to make this faster. For that, I tried to use intersect but it is slower than unique. I downloaded count_unique from http://www.mathworks.com.au/matlabcentral/fileexchange/23333-determine-and-count-unique-values-of-an-array. But this only removes repeating elements and sorts but does not provide index. I tried tip from http://www.mathworks.com/matlabcentral/answers/127660-how-would-i-create-my-own-unique-function-without-using-the-built-in-unique and created myUnique using sort but again it does not provide index. Please let me know if there exist any other option to make this operation faster. Many thanks.
  1 Comment
Uday
Uday on 20 Apr 2015
Can you paste the data and the code to visualize what you are trying to explain ?

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 20 Apr 2015
Edited: Sean de Wolski on 20 Apr 2015
Perhaps I'm missing something but on my machine (fairly wimpy laptop) this is taking a few hundredths of a second
tic
for ii = 1:100
[uv,id] = unique(randi(400,1000,1));
end
toc
Elapsed time is 0.013925 seconds
So I doubt unique is the bottleneck and even if it is, I doubt you'll find a faster solution without C or Fortran hardwired to your inputs. Have you profiled your code?
docsearch profile code

Products

Community Treasure Hunt

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

Start Hunting!