【问题标题】:Matlab equivalent to Python's "+=" operator for sudoku problem [closed]Matlab 等效于 Python 的“+=”运算符用于数独问题 [关闭]
【发布时间】:2020-09-02 03:16:42
【问题描述】:

我正在为 Python 中的运算符 += 寻找一个 Matlab 等效函数。我一直在寻找,我知道在 Matlab 中没有真正的等价物,但我不明白为什么。但是,我正在寻找一种相同的方法或函数。它用于通过回溯法解决数独。它以 9x9 矩阵开始,并生成半随机数,直到 Seq0=0(不再有零)。这是我到目前为止的代码,它没有抛出任何错误,但不能正常工作,因为不做累加操作。

clc;
S=[0,0,0,0,0,0,0,7,6; 0,2,0,9,0,0,0,0,0; 0,3,8,0,5,4,1,0,0; 
    9,0,0,5,0,0,0,3,0; 0,0,0,0,1,8,0,6,7; 4,0,0,0,0,0,2,0,0;
    7,1,3,8,6,2,0,5,0; 5,0,3,6,2,7,0,0,0; 6,0,2,0,3,0,8,9,4]

hS1=S(1,1:9); hS2=S(2,1:9); hS3=S(3,1:9);
hS4=S(6,1:9); hS5=S(5,1:9); hS6=S(6,1:9);
hS7=S(7,1:9); hS8=S(8,1:9); hS9=S(9,1:9);

L9=1:9; iteraciones=0;
S1=(reshape(S,1,[])); S1eq0=~ismember(S1,L9);
Seq0=1; remS=find(S1eq0); lenS=length(remS)

while Seq0 > 0
    iteraciones=iteraciones+1
    for kk=9:89
        fila=fix(kk/9);
        columna=rem(kk,9)+1;
        if columna<=9 && columna<=9
            if S(fila, columna)==0
                for value=1:9
                    if ~ismember(value,S(fila,:))
                        if ~ismember ( value,cat(2, hS1(:,columna),...
                                hS2(:,columna), hS3(:,columna),...
                                hS4(:,columna), hS5(:,columna),...
                                hS6(:,columna), hS7(:,columna),...
                                hS8(:,columna), hS9(:,columna)) )
                            square1 = [];
                            square2 = [];
                            square3 = [];
                            if fila<=3
                                if columna<=3
                                    for ii=1:3
                                        square1=[S(ii,1:3)]; %Here is the problem
                                    end
                                elseif columna<=6
                                    for ii=1:3
                                        square1=[S(ii,4:6)]; %here is just the same
                                    end
                                else
                                    for ii=1:3
                                        square1=[S(ii,7:9)];
                                    end
                                end

                            elseif fila<=6
                                if columna<=3
                                    for ii=4:6
                                        square2=[S(ii,1:3)];
                                    end
                                elseif columna<=6
                                    for ii=4:6
                                        square2=[S(ii,4:6)];
                                    end
                                else
                                    for ii=4:9
                                        square2=[S(ii,7:9)];
                                    end
                                end

                            else
                                if columna<=3
                                    for ii=7:9
                                        square3=[S(ii,1:3)];
                                    end
                                elseif columna<=6
                                    for ii=7:9
                                        square3=[S(ii,4:6)];
                                    end
                                else
                                    for ii=7:9
                                        square3=[S(ii,7:9)];
                                    end
                                end

                            if ~ismember(value, cat(2,square1,square2,square3))
                                S(fila,columna)=value;
                            end
                            end
                        end
                    end
                end
            end
        end
    end
    S1=(reshape(S,1,[])); S1eq0=~ismember(S1,L9);
    remS=find(S1eq0); Seq0=length(remS)
    S
    pause(2.0)
end

【问题讨论】:

  • a += ba = a + b 完全相同。 += 是所谓的“语法糖”,很好,但从来没有必要。您必须更好地解释问题所在,

标签: python matlab matrix increment sudoku


【解决方案1】:

通常你可以这样解决: var = var + newVal.

Python:a += 5

Matlab:a = a + 5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多