How to change my code to include a while loop?

I generated this code and was wondering if there was any way to have it to the same function with using a while loop?

2 comentarios

Matthew
Matthew el 1 de Feb. de 2013
Editada: Walter Roberson el 3 de Feb. de 2013
function B=Sudoku(A)
[m,n]=size(A);
if m~=n
error('The matrix is not square')
end
R=sum(A,1);
C=sum(A,2);
D=abs(diff(R));
E=abs(diff(C));
if any(D)||any(E) > 10^-7
error('the sums are not the same')
else if all(D)&& all(E) <10^-7
disp(C(1,1))
end
end
B=C(1,1);
Cedric
Cedric el 1 de Feb. de 2013
Editada: Cedric el 1 de Feb. de 2013
function B=Sudoku(A)
[m,n] = size(A);
if m ~= n
error('The matrix is not square')
end
R = sum(A, 1) ;
C = sum(A, 2) ;
D = abs(diff(R)) ;
E = abs(diff(C)) ;
if any(D) || any(E) > 10^-7
error('the sums are not the same')
else
if all(D) && all(E) < 10^-7
disp(C(1,1))
end
end
B = C(1,1);
Your conditional statements are likely not to be correct actually; for example
all(D) && all(E) < 10^-7
will be evaluate as the AND (short-circuit) between array D (where zero elements are interpreted as logical false and the others as true) and the outcome of the test with the relational operator (which is a logical array of element-wise < 10^-7, based on E).
What would you like to "loop" in this function?

Respuestas (0)

La pregunta está cerrada.

Etiquetas

Preguntada:

el 1 de Feb. de 2013

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by