How to detect patterns in a matrix

45 views (last 30 days)
Anna van der Stap
Anna van der Stap on 16 Dec 2020
Commented: Image Analyst on 21 Dec 2020
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

Answers (1)

Image Analyst
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
Anna van der Stap
Anna van der Stap on 17 Dec 2020
Sorry I feel that is difficult to explain in a simple manner.
Let me explain my situation more clearly from scratch. The variable in question is called spikes_per_bin, and it contains the number of spikes on an ecoG recording that take place in a certain time bin (0.01 seconds long). When a seizure is taking place there is a rhythmic activity, which means on spikes per bin you end up with a few bins with spikes in (non zero elements) and then a stretch of 5/6 time bins without spikes in there (zero elements). This then repeats itself for the duration of that seizure (duration is a variable i also want to look into so I can't give a definitive answer on how long this would be). During movement, there is no rhytmicity to the spiking, which is when you end up with a situation as shown above where there are a random number of zero elemetns followed by a random number of non zero elements. What I want to do is to extract the indices of the time bins in which this rhythmic activity is taking place. So it's not that I'm looking for individual runs, but more for areas that contain that overall pattern with the "hard and fast" 3,4,5,6. Does this sort of make more sense?
Image Analyst
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

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!