How to detect patterns in a matrix
23 views (last 30 days)
Show older comments
Hello,
I am trying to look for patterns in my data. The situation I have is a 341500x3 double, of which I have posted a region of interest below. Certain regions of interest have this kind of pattern where there is a string of 5/6 zeros, followed by 3 or 4 numbers. This repeats several times. I am trying to find the index of where this takes place. The rest of the data doesn't show this sort of pattern. I have tried extracting the index position of non zero elements but I still can't figure out how to look for this pattern. Thanks in advance.
0 0
22 26
23 29
9 5
0 0
0 0
0 0
0 0
0 0
0 0
6 2
22 25
10 14
5 11
0 0
0 0
0 0
0 0
0 0
0 0
0 0
24 6
14 13
52 43
0 0
0 0
0 0
0 0
0 0
0 0
0 0
21 28
21 22
0 0
0 3
0 0
0 0
0 0
0 0
0 0
0 0
0 0
9 11
18 22
3 8
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
14 12
0 0
20 31
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
20 17
2 3
0 0
0 0
0 0
0 0
0 0
0 0
0 0
8 7
4 6
0 Comments
Answers (1)
Image Analyst
on 16 Dec 2020
I'm not sure how fixed the 5 and/or 6 zeros and 3 and/or 4 numbers are. For example is the row of zeros and two rows of numbers like at the top of your matrix meeting the criteria, or not? Does this do what you want:
m=[...
0 0
22 26
23 29
9 5
0 0
0 0
0 0
0 0
0 0
0 0
6 2
22 25
10 14
5 11
0 0
0 0
0 0
0 0
0 0
0 0
0 0
24 6
14 13
52 43
0 0
0 0
0 0
0 0
0 0
0 0
0 0
21 28
21 22
0 0
0 3
0 0
0 0
0 0
0 0
0 0
0 0
0 0
9 11
18 22
3 8
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
14 12
0 0
20 31
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
20 17
2 3
0 0
0 0
0 0
0 0
0 0
0 0
0 0
8 7
4 6]
mask = m(:, 1) > 0;
props = regionprops(mask, 'PixelIdxList');
% Get the starting index of non-zeros into a vector.
for k = 1 : length(props)
startingRow(k) = props(k).PixelIdxList(1);
fprintf('Run %d starts at row %d.\n', k, startingRow(k));
end
fprintf('Done running %s.m ...\n', mfilename);
It gets the starting point af any non-zero run regardless of how long it is or how many zeros separate it from the adjacent runs. You get
Run 1 starts at row 2.
Run 2 starts at row 11.
Run 3 starts at row 22.
Run 4 starts at row 32.
Run 5 starts at row 43.
Run 6 starts at row 54.
Run 7 starts at row 56.
Run 8 starts at row 65.
Run 9 starts at row 74.
6 Comments
Image Analyst
on 21 Dec 2020
You can use normxcorr2() to look for well predefined patterns in a vector. I believe it will work for a 1-D "image" (signal) as well as a 2-D image. I'm attaching my demo. See if you can adapt this to use your 3,4,5,6 template, and your signal (instead of my demo image).
Otherwise you can just scan your signal looking for a match, something like (untested)
template = [3,4,5,6];
matches = false(1, length(signal))
for k = 1 : length(signal) - length(template)
matches(k) = true;
for k2 = 1 : length(template)
if signal(k + k2 - 1) ~= template(k2)
matches(k) = false;
break;
end
end
end
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!