Quickly Finding Numeric Patterns in MATLAB

Comrade programmers!

About five years ago I wrote a little program to help me find numeric patterns in arrays. Basically I needed to find byte patterns like 0xFFFE 0xE000 in the data from DICOM files. Something like MATLAB’s strfind function, except more general than looking for substrings in strings.

So I wrote a rather naïve algorithm to do just that. It wasn’t very fast, but it worked for exploratory use. But then today I needed to do something like that in production code. So I asked one of my coworkers for tips with a faster implementation.

Turns out the strfind works just as well on numeric arrays as it does on strings. And it’s really fast, too.

% "data" is a 1,200,000 element UINT8 array.
% It contains 29 instances of the pattern.
tic; offsetTable = strfind(data, [254 255 0 224]); toc

% Elapsed time is 0.027390 seconds.

Update: Loren Shure posted an even better solution on her blog.

This entry was posted in Computing, MATLAB. Bookmark the permalink.

2 Responses to Quickly Finding Numeric Patterns in MATLAB

  1. tom tom says:

    What does thissequence represent?

  2. Jeff Mather says:

    Those numbers came from your pager, am I right?

    I believe that if you look at a telephone keypad and then pivot the numbers around the “5″ in the middle, you’ll see the phone number you need to call back and the code for the person who called you.

    (Apologies to anyone who hasn’t yet watched “The Wire.”)

    In all seriousness, his post was about finding specific runs of numbers that exist in a larger sequence, not finding any deeper meaning.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>