Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Why do these lines of code not function properly?

1 visualización (últimos 30 días)
Mohannad Abboushi
Mohannad Abboushi el 6 de Mayo de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
In this function the first if statement does not work when I provide a matrix with elements larger than 4. I have tried using for loops but that effects the rest of the program for some reason. Also when elseif any(diff(sort(m(:,1))==0)) does in fact equal zero the out is not returned as zero but instead made into one. I spent hours trying to debug it, but I am still having issues. Any assistance is appreciated, thank you.
function out=issudoku(m)
[R,C]=size(m);
if m()>4
out=0;
else if R~=4 || C~=4
out=0;
%Find if any number on the board is not a 1, 2, 3 or 4
%%%see if any number is repeated within the individual squares
elseif m(1,1)==m(2,1)||m(1,1)==m(1,2)||m(1,1)==m(2,2)
out=0;
elseif m(3,1)==m(4,1)||m(3,1)==m(3,2)||m(3,1)==m(4,2)
out=0;
elseif m(1,3)==m(2,3)||m(1,3)==m(1,4)||m(1,3)==m(2,4)
out=0;
elseif m(3,3)==m(4,3)||m(3,3)==m(3,4)||m(3,3)==m(4,4)
out=0;
%repeated numbers within columns:
elseif any(diff(sort(m(:,1))==0))
out=0;
elseif any(diff(sort(m(:,2))==0))
out=0;
elseif any(diff(sort(m(:,3))==0))
out=0;
elseif any(diff(sort(m(:,4))==0))
out=0;
%repeated numbers within rows:
elseif any(diff(sort(m(1,:))==0))
out=0;
elseif any(diff(sort(m(2,:))==0))
out=0;
elseif any(diff(sort(m(3,:))==0))
out=0;
elseif any(diff(sort(m(4,:))==0))
out=0;
else
out=1;
end
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de Mayo de 2016
Perhaps you want
if any(m>4)
??
  2 comentarios
Mohannad Abboushi
Mohannad Abboushi el 6 de Mayo de 2016
I tried that and it doesn't do anything at all
Mohannad Abboushi
Mohannad Abboushi el 6 de Mayo de 2016
Editada: Mohannad Abboushi el 6 de Mayo de 2016
I changed the code a bit and tried debugging it for why out does not become false when this matrix is provided: [1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]. The code seems valid, but it skips over the if statement when a~=validnumbers. What's going on??
function out=issudoku(m)
out=true;
validnumbers=[1,2,3,4];
[R,C]=size(m);
%check if sudoku board is 4x4
if R~=4||C~=4
out=false;
end
%check if any elements are greater than 4
if any(m>4)
out=false;
end
%check for any repeat values in columns
for j=1:C
a=m(:,j);
a=sort(a');
if a~=validnumbers
out=false;
end
end
%check for any repeat values in rows
for i=1:R
b=m(i,:);
sort(b);
if b~=validnumbers
out=false;
end
end
%check if any number is repeated within the individual squares
submatrix1=m([1 2],[1 2]);
if sort(reshape(submatrix1,1,[]))~=[1 2 3 4]
out=false;
end
submatrix2=m([3 4],[1 2]);
if sort(reshape(submatrix2,1,[]))~=[1 2 3 4]
out=false;
end
submatrix3=m([1 2],[3 4]);
if sort(reshape(submatrix3,1,[]))~=[1 2 3 4]
out=false;
end
submatrix4=m([3 4],[3 4]);
if sort(reshape(submatrix4,1,[]))~=[1 2 3 4]
out=false;
end

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by