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)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
Walter Roberson
el 6 de Mayo de 2016
Perhaps you want
if any(m>4)
??
2 comentarios
Mohannad Abboushi
el 6 de Mayo de 2016
Editada: Mohannad Abboushi
el 6 de Mayo de 2016
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!