How to replace a vector element with a value not equal the other vector values?

I struggling with an assignemt in which i have to find the remaining number in a single sudoku row. I have to replace 0 with a number which doesnt match the current numbers i the row.
my current is:
function sudokuRow = fillSudokuRow(sudokuRow)
sudokuRow(sudokuRow==0)=a;
I can make it replace the 0 with a secpific number but i cant figure out how to make variate numbers depending on the numbers in the suduko row?

Answers (1)

function sudokuRow = fillSudokuRow(sudokuRow)
idx=(sudokuRow==0);
sudokuRow(idx)=func(sudokuRow(~idx));
end

8 Comments

Thx Matt! allthough it doesnt seem to work. When inserting vector input (sudokuRow) this error accours:
Unrecognized function or variable 'func'.
Error in fillSudokuRow (line 4)
sudokuRow(idx)=func(sudokuRow(~idx));
It doesn't recognize the function func(), because you haven't written it yet!
Code:
function sudokuRow = fillSudokuRow(sudokuRow)
idx=(sudokuRow==0);
sudokuRow(idx)=func(sudokuRow(~idx));
end
Output:
>> fillSudokuRow([9,4,0,1,5,7,2,3,8])
Unrecognized function or variable 'func'.
Error in fillSudokuRow (line 4)
sudokuRow(idx)=func(sudokuRow(~idx));
You haven’t showed or perhaps not defined the function func() as Matt mentions in his comments.
Im sorry very new to this.. How do you do that?
@Joakim,
To be a bit clearer, your question asks about how to extract the non-zeros and do "something" with them, and that's what my answer demonstrates how to do, in the same generality in which your question is phrased. But your post does not say that you see a difficulty even after you have the non-zeros, nor what that difficulty is.
You also seem to think that we can offer more help with no explanation about what func() is required to do. But I don't know why we should know what comes next better than you. Would it be clear to someone familiar with Sudoku? I myself have never played....
I see what you mean Matt J. I thought it was clear but i see that my question could go in many directions, im still not to comfortable in how to ask the right questions as i'm not to familiar with the terms within Matlab.
I solved the problem in the end, with great help from you guys. thx.
function sudokuRow = fillSudokuRow(sudokuRow)
idx=(sudokuRow==0);
sudokuRow(idx)=45-(sum(sudokuRow));
end
output:
fillSudokuRow([9,4,0,1,5,7,2,3,8])
ans =
9 4 6 1 5 7 2 3 8

Sign in to comment.

Categories

Find more on Sudoku in Help Center and File Exchange

Asked:

on 15 Feb 2020

Commented:

on 17 Feb 2020

Community Treasure Hunt

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

Start Hunting!