# find the equal values that stand together on the same row or column or diagonal in the matrix

12 views (last 30 days)
Mai Thành on 4 Jan 2022
Commented: Jon on 7 Jan 2022
I have a 15x15 matrix that contain 3 values which is -1, 1 and 2. The position of those values are random. I want to spot out cases that 1 or 2 are consecutive in horizontal, vertical and diagonal. ( The rules nearly the same as tic tac toe game )
I tried to use the function below ( I forget the right way to call it) and it takes me a lot of time. Is anyone have sugestion or solution to solve this problems faster, please help me
%horizontal
%line 1
m(1,1) == m(1,2) && m(1,2) == m(1,3) && m(1,3) == m(1,4) && m(1,4) == m(1,5) && m(1,5) ~= -1
m(1,2) == m(1,3) && m(1,3) == m(1,4) && m(1,4) == m(1,5) && m(1,5) == m(1,6) && m(1,6) ~= -1
m(1,3) == m(1,4) && m(1,4) == m(1,5) && m(1,5) == m(1,6) && m(1,6) == m(1,7) && m(1,7) ~= -1
m(1,4) == m(1,5) && m(1,5) == m(1,6) && m(1,6) == m(1,7) && m(1,7) == m(1,8) && m(1,8) ~= -1
m(1,5) == m(1,6) && m(1,6) == m(1,7) && m(1,7) == m(1,8) && m(1,8) == m(1,9) && m(1,9) ~= -1
m(1,6) == m(1,7) && m(1,7) == m(1,8) && m(1,8) == m(1,9) && m(1,9) == m(1,10) && m(1,10) ~= -1
m(1,7) == m(1,8) && m(1,8) == m(1,9) && m(1,9) == m(1,10) && m(1,10) == m(1,11) && m(1,11) ~= -1
m(1,8) == m(1,9) && m(1,9) == m(1,10) && m(1,10) == m(1,11) && m(1,11) == m(1,12) && m(1,12) ~= -1
m(1,9) == m(1,10) && m(1,10) == m(1,11) && m(1,11) == m(1,12) && m(1,12) == m(1,13) && m(1,13) ~= -1
m(1,9) == m(1,10) && m(1,10) == m(1,11) && m(1,11) == m(1,12) && m(1,12) == m(1,13) && m(1,13) ~= -1
m(1,10) == m(1,11) && m(1,11) == m(1,12) && m(1,12) == m(1,13) && m(1,13) == m(1,14) && m(1,14) ~= -1
m(1,11) == m(1,12) && m(1,12) == m(1,13) && m(1,13) == m(1,14) && m(1,14) == m(1,15) && m(1,15) ~= -1
Mai Thành on 5 Jan 2022
I don't know how to explain it clearer for you. Sorry for my bad English. But, thank you for your help to question, I really appreciate it

Matt J on 4 Jan 2022
Edited: Matt J on 4 Jan 2022
e=ones(1,5);
I=eye(5);
bool=false;
for i=1:2
bool=bool | any( conv2(mask,e ,'valid' )>=5 ,'all'); %any horizontal
bool=bool | any( conv2(mask,e','valid' )>=5 ,'all'); %any vertical
bool=bool | any( conv2(mask,I ,'valid' )>=5 ,'all'); %any diagonal down
bool=bool | any( conv2(mask,fliplr(I) ,'valid' )>=5 ,'all'); %any diagonal up
end
if bool,
result="Yes"
else
result="No"
end
Jon on 6 Jan 2022
Thanks!

Jan on 4 Jan 2022
Edited: Jan on 4 Jan 2022
Maybe you mean:
pool = [-1, 1, 2];
index = randi([1, 3], 15, 15);
data = pool(index);
match = conv2(data > 0, ones(3, 3), 'same') == 5;
% or >= 5 ?
% Or:
match = conv2(data == 1, ones(3, 3), 'same') >= 5 || ...
conv2(data == 2, ones(3, 3), 'same') >= 5;
Jon on 7 Jan 2022
Hi Jan,
Thanks for the clarification. That makes perfect sense. Between your answer and @Matt J's I learned a lot about how you can find patterns using conv2 - good stuff! I've learned a lot and picked up a lot of good tricks from your many posts, thanks so much

Jon on 5 Jan 2022
I am assuming here that to satisfy your criteria you must have a group of 5 or more ones in any row, column or diagonal or a group of 5 or more twos in any row, column or diagonal, but they can't be mixed. So for example here is a row that satisfies the criteria:
[0 0 2 2 2 2 2 0 0 0 1 0 0 2 2]
and here is one that would not satisfy the criteria:
[0 0 1 2 2 1 1 0 0 0 1 0 0 2 2]
I think the following code will do what you ask:
% find if there are rows, columns, or diagonals with specified number of
% repeated elements which match a given value or values
% define the parameters
matchVals = [1,2]; % values to be matched
numReq = 5; % number of repeated elements that are required
elements = [-1,1,2]; % possible elements in matrix
dims = [15,15]; % matrix dimensions
% example matrix
A = elements(randi(3,dims(1),dims(2)));
disp(A)
% check for repeated runs of each element to be matched
maxCount = 0;
for k = 1:numel(matchVals)
% make a matrix with zeros and ones with ones where values match input list
M = ismember(A,matchVals(k));
% check repeat counts for columns, rows, and diagonals
% Note: spdiags makes matrix whose columns are non-zero diagonals of M
count = [
repcount(M);
repcount(M');
repcount(spdiags(M)); % diagonals left to rigth
repcount(spdiags(flip(M)))]; % diagonals right to left
maxCount = max(maxCount,max(count));
end
% check if required number of repeats occurs
if maxCount >= numReq
disp('Yes')
return
else
disp(['No max count = ',num2str(maxCount)])
end
function count = repcount(A)
% get dimensions
n = size(A,2);
% add zero rows to provide breaks at top and bottom of each column
A = [zeros(1,n);A;zeros(1,n)];
% mark begin and end of groups of ones with 1's and -1's
% operate columwise
delta = diff(A(:));
% make ramps to count up where ones occur
ramp = cumsum(A(:));
% get individual counts for each group
counts = ramp(delta == -1) - ramp(delta == 1);
% find longest group
count = max(counts);
end
##### 2 CommentsShowHide 1 older comment
Mai Thành on 6 Jan 2022