fastmode is an improvement on 'mode.m' by Michael Robbins (File ID 5266). It ignores NaN's and properly returns multiple results if more than one element occurs at equal frequency.
M-file profiler revealed extracting unique values took 50% of the time spent in the function, and since a list of unique values were not required, a work-around has been used that makes this even faster than the first submission (25 seconds to process a 100,000 element vector iterated 1,000 times, versus original 27 seconds and Robbins' hist-based version of 69 seconds).
The function is rather robust in that it can handle even single-element vectors, but it will generate an error if you attempt to call it with an empty vector. Since it's designed for speed, no error checking is performed on the input.
You are correct that the method I used can, sometimes, bias towards the first if there is some sort of caching going on. If you review the comments I gave to the other mode.m file (5266) you will note I ran the same test swapping the order several different ways without any real changes. I was lazy this time and neglected to do so, mostly because I didn't see any potential caching of results.
sorry, I now realised that what you call custmode is the file I submitted. And I just swapped the for loops without changing the et.mode ....lines. You are right; fastmode is really fast. Following are the results now
Well, to my experience the method you use for speed comparison is biased towards the code entered first; that's why I never use this method. I swapped the for loops for mode and fastmode (I am excluding custmode becuase it does not exist in ML list) and I had the following results:
My cpu is Pentium Core Duo 1.2Ghz
Forgot to mention, in previous comment, "custmode" is file 5826. Also, as expected, the results are the same for all 3 trials. What differs, however, are the following:
By design, MATLAB's mode returns only the smallest value if there are multiple entries with equal frequency. Both fastmode and custmode return all values (custmode had an issue with column vectors).
Also by design, MATLAB handles empty input whereas fastmode does not (see instructions for fastmode). custmode also fails to handle empty vectors.
No, I did not see mode.m (5826) before. However, I have just checked the speed using the following command line:
% Time first routine
% Time second routine
% Time internal routine
running MATLAB 2006a on a Pentium Core Duo 2.4Ghz machine with the following results:
'fastmode' is designed to be called from a loop like the command above - optimized for speed, not ease of use.
Have you checked mode.m (file id 5826) I submitted in 2004? It does exactly what is claimed here? Also check the speed.