Clear Filters
Clear Filters

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

6 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 17 Apr 2016
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.

More Answers (0)

Categories

Find more on Sudoku in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!