Comments on: Finding Patterns in Arrays https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Fri, 09 Dec 2011 02:19:27 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Dave https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32823 Fri, 09 Dec 2011 02:19:27 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32823 Hi Loren et al.,
As always, thanks for the great post! How about extending to the case where you don’t know the exact pattern, but do know something about the pattern. For example, find the longest set of sequential numbers in a monotonically increasing array of integers. For example, in the sequence

a = [1 2 14 15 16 17 18 29 30 41 44 45],
,
the longest sequential set is 14:18. One vectorized way I came up with is:

b = find([1 ~(diff(a)==1) 1])
[~,c] = max(diff(b))
a(b(c):b(c+1)-1)

But I’d love to hear of more elegant solutions. Thanks!

]]>
By: Malcolm Lidierth https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32670 Wed, 16 Nov 2011 12:48:31 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32670 @MN
If the original data are only 2 or 3 bytes long, which I think is the case with MIDI, could you not typecast a to get 2 values per element then use find:
You may need to pad the end of a with zeros to align the data, then e.g

a = [0 1 4 9 16 4 9 0 7 8 4 9 4 9 7 8 9 4 9 10];
a=uint32(a);
b=typecast(a,'uint64');
% find odd numbered elements...
idxodd=(find(b==typecast(uint32([4,9]),'uint64'))-1)*2+1;
%then even
b=typecast([uint32(0) a uint32(0)], 'uint64');
idxeven=(find(b==typecast(uint32([4,9]),'uint64'))-1)*2;
% Both
idx=sort([idxodd idxeven])
]]>
By: Loren https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32662 Tue, 15 Nov 2011 21:25:40 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32662 MN-

I can’t think of an elegant way to do that. Is it only 2 character matches you are looking for?

Just thinking. Here are 2 possible approaches.
1) You might create a new matrix with one row offset by one location (left or right, and padded to get all elements to be comparable) from the other row. Transposing those into a nx2 array, you could then look for matched rows by calling intersect with the rows option perhaps. But I haven’t thought hard about this.
2)Something similar where you use diff to check…

–Loren

]]>
By: MN https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32660 Tue, 15 Nov 2011 21:19:32 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-32660 Can you also find patterns without specifying the input?
If a = [0 1 4 9 16 4 9];
suppose I just wanted to find any pattern in which two adjacent characters repeat. The answer here would be: Pattern 4 9 and placement 3 6.
How would you do that?
I’m actually working on a music analysis problem looking for melodic patterns in midi data.

Thanks,
MN

]]>
By: Loren https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-31622 Sun, 29 Aug 2010 15:42:37 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-31622 Stuart-

You have to define the criterion for choosing which one fits best – then compare the new data to each row in the dateset you already have.

–Loren

]]>
By: Stuart Garner https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-31621 Sat, 28 Aug 2010 21:27:32 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-31621 Suppose I had a data matrix with 8 rows and 800 columns, representing the results of 800 experiments with 8 variables each – call the 8 variables age, height, weight, etc. Now suppose someone hands me a new column containing 8 data, in the same order as those in the matrix. How do I find which of the other 800 columns best “matches” the new column?

]]>
By: Phil https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-31580 Thu, 12 Aug 2010 16:47:23 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-31580 I found this really helpful, thanks. Is there a fast and easy way for searching patterns that extent over multiple rows and colums?

such as pattern=[0 1;0 0] in

[0 1 1 1; 0 0 1 1; 0 0 0 1; 0 0 0 0]. The search should fined three locations for such a pattern.

Any ideas?
Phil

]]>
By: Loren https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-30605 Mon, 14 Sep 2009 11:27:17 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-30605 Rameez-

That’s not the sort of pattern I was testing here initially. What I would do with your situation is something like this. Convert the array to logicals, where true represents data in the correct range. Then look for a long-enough string on true values. Get that index. Voila.

–Loren

]]>
By: Rameez Khan https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-30604 Mon, 14 Sep 2009 11:22:48 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-30604 Hello,

Nice blog, and it proved quite useful to me as a student.

I have a query, that if I am not sure of the exact no. of values that may occur for a range based pattern, then how will it may be done.

for. e.g.

I need to check for a pattern whose values may be between -3.7 to +3.7 and the no. of values in between them too aren’t fixed as well.

I have data from accelerometer against time. Which reads at 100Hz. So, I have alot data of the form for e.g:

time = [0.0000 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007 0.0008 0.0009.. ]
and
acc = [ 2.345 3.345 6.454 6.545 -3.5 1.5 2.6 6 2.5 99 32 -3.7 1.3 -2 2.7 … ]

Now I want to check for a pattern of values which stays between -3.7 to + 3.7 for a duration 0.05 seconds or greater not less that 0.05 seconds. I want of find the start and end time of the pattern.

thanks and kind regards,

/Rameez.

]]>
By: Krishna https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-30464 Sat, 11 Jul 2009 10:19:13 +0000 https://blogs.mathworks.com/loren/2008/09/08/finding-patterns-in-arrays/#comment-30464 TRY strmatch.

Test how fast it can be

]]>