Finding a specific pattern in data
Show older comments
Hello all,
I would like to create a routine to identity if a specifi pattern exists in may data.
So far I did this:
data=somematrix;
% Define the pattern to search for
pattern = [3, 4, 5];
% Initialize a flag to check if the pattern is found
patternFound = false;
% Iterate through the data with a sliding window
for i = 1:(length(data) - length(pattern) + 1)
% Extract a subsequence of the same length as the pattern
subsequence = data(i:i+length(pattern)-1);
% Compare the subsequence with the pattern
if isequal(subsequence, pattern)
patternFound = true;
break; % Exit the loop if the pattern is found
end
end
% Display the result
if patternFound
disp('Pattern Found in the Data.');
else
disp('Pattern Not Found in the Data.');
end
This works when the data is a vector but is not working if instead a vector I'm working with a matrix.
Thanks in advance.
1 Comment
Dyuman Joshi
on 10 Nov 2023
How should the pattern be detected in the matrix?
Horizontally? Vertically? Diagonally? Or reverse of the orientations mentioned?
Answers (1)
Johnny
on 10 Nov 2023
Hello Ricardo,
You can use the ismember function (https://www.mathworks.com/help/matlab/ref/double.ismember.html) combined with logical operations.
patternFound = false ;
% Check if pattern is a subset of data
if size(data,2) < size(pattern,2)
% pattern cannot be found: column number in pattern is greater than column number in data
patternFound = false ;
elseif size(data,2) == size(pattern,2)
% Same number of column between pattern and data
patternFound = all(ismember(pattern, data, 'rows'));
else
% column number in data is greater than column number in pattern
% Extract submatrices from data, to have the same number of columns
% for comparison and loop through the different combinations.
for k = 1:(size(data,2)-size(pattern,2)+1)
patternFound = all(ismember(pattern, data(:, k:(k+size(pattern, 2)-1)), 'rows'));
if patternFound
break
end
end
end
%
if patternFound
disp('Pattern Found in the Data.');
else
disp('Pattern Not Found in the Data.');
end
6 Comments
Ricardo Duarte
on 11 Nov 2023
Dyuman Joshi
on 13 Nov 2023
Edited: Dyuman Joshi
on 13 Nov 2023
So for the pattern - [3 4 5]
The accepted values should be - [3+- 5% 4+-5% 5+-5%] ?
Also, the pattern should only be matched with the same orientation? or any other/specific orientation(s)?
Ricardo Duarte
on 14 Nov 2023
Johnny
on 15 Nov 2023
Depending on the dataset you have, you can use ismembertol instead of ismember. You can refer to the documentation for more information.
Another approach is to update your initial code to work on matrices.
% Check if pattern is a subset of data
tol = 0.05 ; % Update the tolerance as needed
patternFound = false ;
if size(data,2) < size(pattern,2)
patternFound = false ;
elseif size(data,2) == size(pattern,2)
lowerLimit = data >= (1-tol)*pattern ;
upperLimit = data <= (1+tol)*pattern ;
patternFound = all(upperLimit & lowerLimit,"all") ;
else
for k = 1:(size(data,1)-size(pattern,1)+1)
for l = 1:(size(data,2)-size(pattern,2)+1)
% Extract a subdata matrix which dimensions match the pattern
subdata = data(k:(k+size(pattern,1)-1),l:(l+size(pattern,2)-1)) ;
% Use logical operations to determine if the data is within
% range
lowerLimit = subdata >= (1-tol)*pattern ;
upperLimit = subdata <= (1+tol)*pattern ;
patternFound = all(upperLimit & lowerLimit,"all") ;
if patternFound
break
end
end
if patternFound
break % double break to exit nested loops
end
end
end
%
if patternFound
disp('Pattern Found in the Data.');
else
disp('Pattern Not Found in the Data.');
end
For the orientation, I believe @Dyuman Joshi is asking whether you want to look for the pattern as it is, or if you also want to search for its transpose form for example. Note that the code above only works for the same orientation.
Dyuman Joshi
on 15 Nov 2023
@Ricardo Duarte, see the below example -
arr = [3 4 6
4 4 6
5 6 5];
pattern = [3 4 5];
In the array, the pattern is found - but vertically and diagonally, which is not the same the orientation as of the pattern, that is horizontal.
So, here, should the pattern be considered to be found, or not?
Ricardo Duarte
on 15 Nov 2023
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!