Matlab相当于数独问题的Python“ + =”运算符

问题描述

我正在寻找Python中运算符+=的Matlab等效函数。我一直在寻找,但我知道Matlab中没有真正等同的东西,但是我不明白为什么。但是,我正在寻找一种方法或函数。用于通过回溯方法解决数独问题。它以9x9矩阵开始,并生成半随机数,直到Seq0=0(不再有零)为止。到目前为止,这是我的代码,它不会引发任何错误,但是由于不执行累积操作而无法正常工作。

clc;
S=[0,7,6; 0,2,9,0; 0,3,8,5,4,1,0; 
    9,6,7; 4,0;
    7,0; 5,0; 6,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,[])); 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(:,hS3(:,...
                                hS4(:,hS5(:,...
                                hS6(:,hS7(:,...
                                hS8(:,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,square1,square2,square3))
                                S(fila,columna)=value;
                            end
                            end
                        end
                    end
                end
            end
        end
    end
    S1=(reshape(S,L9);
    remS=find(S1eq0); Seq0=length(remS)
    S
    pause(2.0)
end

解决方法

通常,您可以解决以下问题: var = var + newVal

Python:a += 5

Matlab:a = a + 5

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...