Help Coding Sudoku Constraints for Project

2 views (last 30 days)
Shawn Hunt
Shawn Hunt on 7 May 2019
Edited: per isakson on 7 May 2019
Im doing a project about solving sudokus with orthogonal matching pursuit and got stuck trying to write code for one of the constraints.
I understand that the constraint that i'm attempting to write code for should allow for all 81 of the cells to be filled with a value between 1-9. The issue is that i'm not sure how to get started with writing that particular code in matlab.
%%
A = zeros(324,729);
% Row j has one k
for j=1:9
for k=1:9
RowA = (j-1)*9+k;
ColA = 81*(j-1)+(k-1)+[1:9:81];
A(RowA,ColA) = ones(1,9);
end
end
% Box (m,n) has one k
for j=1:9
m = ceil(j/3);
n = 1+mod(j-1,3);
for k=1:9
RowA = 81 + (j-1)*9+k;
ColA = (m-1)*243+(n-1)*27+(k-1)+[1 10 19 82 91 100 163 172 181];
A(RowA,ColA) = ones(1,9);
end
end
% Col j has one k
for j=1:9
for k=1:9
RowA = 162 + 9*(j-1)+(k-1)+[1:9:81];
ColA = 81*(j-1)+k;
A(RowA,ColA) = ones(1,9);
end
end
% Cell (m,n) has one value
%% Cell (m,n) equals given value

Answers (0)

Categories

Find more on Sudoku in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!