How to detect patterns in a matrix

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

回答 (1 件)

Image Analyst
Image Analyst 2020 年 12 月 16 日

0 投票

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 件のコメント

Anna van der Stap
Anna van der Stap 2020 年 12 月 17 日
It throws up an error on line 2:
Undefined function 'regionprops' for input arguments of type 'char'.
Error in Seizure_detection (line 114)
props = regionprops(mask, 'PixelIdxList');
but I have checked and mask is now a 341500x2 logical with a 'true'' value for non zero elements.
Also I am a bit concerned that this is going to give me a lot of responses and not really solve my problem. Here is an example of a bit of the matrix that isn't a region of interest.
0 0
6 6
30 23
15 13
2 4
0 0
8 9
0 0
0 0
0 3
8 7
19 19
41 42
47 36
24 22
23 16
28 29
20 20
12 15
0 0
2 0
0 0
0 0
0 0
0 0
3 0
7 8
0 0
0 0
0 0
0 0
13 16
0 0
0 0
0 0
0 0
0 0
20 14
6 0
12 5
0 0
0 0
0 0
11 4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
2 0
0 0
0 0
0 0
0 0
16 9
0 0
There's still lots of zeros and runs of numbers but it's not in the same pattern of 3/4 numbers followed by 5/6 zeros. If it picks up every run of zeros it's going to pick up a lot of things, including the regions I'm not interested in.
Thank you so much already for your help though, I really appreciate it!
Image Analyst
Image Analyst 2020 年 12 月 17 日
You must not have a license for the Image Processing Toolbox. What does this do:
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % Check for Image Processing Toolbox.
% hasLicenseForToolbox = license('test', 'Image_Acquisition_Toolbox'); % Check for Image Acquisition Toolbox.
% hasLicenseForToolbox = license('test', 'Statistics_Toolbox'); % Check for Statistics and Machine Learning Toolbox.
% hasLicenseForToolbox = license('test', 'Signal_Toolbox'); % Check for Signal Processing Toolbox.
% hasLicenseForToolbox = license('test', 'Video_and_Image_Blockset'); % Check for Computer Vision System Toolbox.
% hasLicenseForToolbox = license('test', 'Neural_Network_Toolbox'); % Check for Deep Learning Toolbox.
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
You can get and install missing toolboxes from the "Add-ons" button on the "Home" tab of the tool ribbon.
Anna van der Stap
Anna van der Stap 2020 年 12 月 17 日
Sorry, I just came to that conclusion as well! I've downloaded it, but as I thought it doesn't work for my needs. The last printed message is run 9007 starts at row 680269. It's picking up absolutely everything. Is there a way to make it more specific?
Image Analyst
Image Analyst 2020 年 12 月 17 日
I don't see an explicit answer to my question. Here it is again:
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?
So are the 3,4,5,6 hard and fast numbers? Like if the runs are 20 non-zero numbers long and separated by a run of 2 zeros from the prior run, you don't want that run? Or if there are 3 or 4 non-zero numbers that come after a stretch of 4 zeros, you don't want that stretch of non-zero numbers? If there is a run of 2 zeros, do you want to exclude both runs on non-zero numbers on either side of the zeros?
Anna van der Stap
Anna van der Stap 2020 年 12 月 17 日
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 2020 年 12 月 21 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2020 年 12 月 16 日

コメント済み:

2020 年 12 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by