version (820 Bytes) by Christopher Harris
Find k occurances of a value in array. Slightly faster than the usual routs.


Updated 26 Jan 2015

View License

Example :
>> a = [ 1 4 5 4 4 7 4 4 9 4 ];
>> find(a==4,5)

ans =

2 4 5 7 8

>> fastFindK(4,a,5)

ans =


I had a situation in which I was looking for indices many many times. Unfortunately, I could not get around the problem via logical arrays or anything of that sort (there was quite a bit of code which I would have to change otherwise). So I did what any rational programmer would do, that is I wasted a good few precious minuets and one or two unnecessary keystrokes to write this lame code.

I recommend just using logical arrays .....

Cite As

Christopher Harris (2022). fastFindK (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!