Help Coding Sudoku Constraints for Project

1 回表示 (過去 30 日間)
Shawn Hunt
Shawn Hunt 2019 年 5 月 7 日
編集済み: per isakson 2019 年 5 月 7 日
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
  1 件のコメント
per isakson
per isakson 2019 年 5 月 7 日
編集済み: per isakson 2019 年 5 月 7 日

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSudoku についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by