find the equal values that stand together on the same row or column or diagonal in the matrix
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
4 comentarios
Jan
el 4 de En. de 2022
"The output that I want is just yes and no" - "yes" or "no" is not clear. Do you mean a string array?
"5 cells that have the same value ( values are not included -1 ) and stand together in the horizontal/ vertical/ diagonal lines" - this is not clear also. It does not matter, if the values are 1 or 2? The corners can have "no" only, because they have less than 4 neighbors?
What is the wanted output e.g. for:
A = [ 1 2 -1; ...
2 1 -1; ...
2 2 2]
Respuesta aceptada
Matt J
el 4 de En. de 2022
Editada: Matt J
el 4 de En. de 2022
e=ones(1,5);
I=eye(5);
bool=false;
for i=1:2
mask=(m==i);
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
6 comentarios
Más respuestas (2)
Jan
el 4 de En. de 2022
Editada: Jan
el 4 de En. de 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;
3 comentarios
Jan
el 6 de En. de 2022
Editada: Jan
el 6 de En. de 2022
You are not missing something. I did not get, what the OP exactly needed. I've interpreted "5 cells that have the same value ( values are not included -1 ) and stand together in the horizontal/ vertical/ diagonal lines" as 5 of 9 elements in a 3x3 block, not as 5 elements in a row or in a column or in a diagonal.
I will noit remove this not matching answer, because it contains the useful examples in your comment.
Jon
el 5 de En. de 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 comentarios
Jon
el 5 de En. de 2022
I think the approach I use above works, but I'm looking at the other answers too. I'm not familiar with the conv2 function. I'd have to study it further, but it looks like it provides a very clean way of looking for the occurence of specific patterns such as the ones you describe.
Ver también
Categorías
Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!