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

6 次查看(过去 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