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
2020 年 12 月 16 日
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
2020 年 12 月 17 日
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
2020 年 12 月 17 日
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
2020 年 12 月 17 日
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 Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!