Why won't my function in matlab changed the input matrix in the original code?

조회 수: 3 (최근 30일)
I am writing the code for a sudoku game. I am working on writing a function for the hint that will display another value for the user in one of the empty spaces in their sudoku matrix. However, when I run the function in the original code, the function changes it within its own code, but then in the main code, the matrix stays the same, and the value stays zero:
function mat = hint(mat,mat2) %input input is the usersudoku matrix and then mat2 is the original/correct and filled out sudoku matrix
vector = find(mat == 0); %find where user has no inputted values
l = length(vector);
element = vector(randi([1,l],1)); %find random element
num = mat2(element);
mat(element) = num; %set correct value in the user matrix from the base matrix to give user a hint
end
Here is how it is used in my main code:
case 5 % Hint
hints = hints+1; % Increase hints variable to write to file
hint(usersudoku, basesudoku)
dispSudoku(usersudoku)
But in the main code, the usersudoku matrix doesn't change and I don't know why

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 17일
usersudoku = hint(usersudoku, basesudoku);
Update in place does not happen just because a variable is updated in a function: the variable also has to be assigned to outside the function.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sudoku에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by