Matlab 达到一定条件后终止递归

Matlab 达到一定条件后终止递归,matlab,recursion,Matlab,Recursion,我一直在试验使用递归的数独解算器。我遇到的问题是,在找到正确的解决方案后,递归函数不会终止,而是继续,直到在每个位置测试每个数字 如何在Matlab中终止这样的循环?函数中的错误条件“Error(“…”)可以中断执行,但决不是解决问题的好办法 下面是一个类似递归的示例代码,为1到4的数字生成所有可能的2个元素向量。我希望它在两个数字都等于2时停止 %possible moves at each position moveMat = zeros([1,2])+3; lineInput = zer

我一直在试验使用递归的数独解算器。我遇到的问题是,在找到正确的解决方案后,递归函数不会终止,而是继续,直到在每个位置测试每个数字

如何在Matlab中终止这样的循环?函数中的错误条件“Error(“…”)可以中断执行,但决不是解决问题的好办法

下面是一个类似递归的示例代码,为1到4的数字生成所有可能的2个元素向量。我希望它在两个数字都等于2时停止

%possible moves at each position
moveMat = zeros([1,2])+3;

lineInput = zeros([1,2]);

%start recursion
recurNumbers(moveMat, 0, lineInput)

function recurNumbers(moveMat, position, lineVariable)

position = position + 1;

%if all numbers are equal to 2 then try to exit the function
if ~all(lineVariable == 2)

    %if all numbers are not equal to 2, try other combination
    if position < length(lineVariable)+1
        for move = 0 : moveMat(position)

            moveMat(position) = move;
            lineVariable(position) = lineVariable(position) + 1;
            recurNumbers(moveMat,position,lineVariable)
            disp(lineVariable)

        end
    end

else

    disp(lineVariable)
    return

end
end
%每个位置可能的移动
moveMat=零([1,2])+3;
lineInput=零([1,2]);
%开始递归
重复编号(移动标记,0,行输入)
函数递归编号(moveMat、位置、行变量)
位置=位置+1;
%如果所有数字都等于2,则尝试退出该函数
如果~all(lineVariable==2)
%如果所有数字都不等于2,请尝试其他组合
如果位置<长度(lineVariable)+1
对于move=0:moveMat(位置)
moveMat(位置)=移动;
lineVariable(位置)=lineVariable(位置)+1;
重复编号(moveMat、位置、行变量)
disp(线性变量)
结束
结束
其他的
disp(线性变量)
返回
结束
结束

现在它将打印向量“[2]”两次,这表明它识别了条件,但“return”不会执行我想象的操作

虽然我不太清楚您想要实现什么,但我假设以下函数满足您的停止标准

function exit_fl = recurNumbers(moveMat, position, lineVariable)

exit_fl = 0;
if (all(lineVariable == 2)) 
  % Show the result and exit
  disp(lineVariable)
  exit_fl = 1;

else
  position = position + 1;
    %if all numbers are not equal to 2, try other combination
    if position < length(lineVariable)+1
        for move = 0 : moveMat(position)

            moveMat(position) = move;
            lineVariable(position) = lineVariable(position) + 1;
            % Receive the exit status of your function
            ex_fl = recurNumbers(moveMat,position,lineVariable);
            if (ex_fl == 1)
              % If the criterion was met, then stop
              exit_fl = 1;
              return
            end
        end
    end
end
end
function exit\u fl=recurNumbers(移动垫、位置、行变量)
退出_fl=0;
如果(全部(lineVariable==2))
%显示结果并退出
disp(线性变量)
出口_fl=1;
其他的
位置=位置+1;
%如果所有数字都不等于2,请尝试其他组合
如果位置<长度(lineVariable)+1
对于move=0:moveMat(位置)
moveMat(位置)=移动;
lineVariable(位置)=lineVariable(位置)+1;
%接收函数的退出状态
ex_fl=重复编号(移动垫、位置、线变量);
如果(ex_fl==1)
%如果符合标准,则停止
出口_fl=1;
返回
结束
结束
结束
结束
结束

尝试使用调试器进入您的函数;这将帮助您找到问题所在。特别是,在循环中调用递归函数,因此无论结果如何,循环的每次迭代都会调用递归函数!你应该在循环中设置一个中断条件为了强调Zep所说的内容,你确实希望在循环中使用
break
,而不是像你在问题中建议的那样使用
error
。谢谢你的建议,但要设置“if all”(lineVariable==2);打破循环中的“结束”仍然没有停止。@RecBCD您的问题不清楚。你写的算法不是“解数独”:它试图增加lineVariable,直到它等于[2]。试着解释这个算法的作用,而不是你最大的方案是什么。它仍然不会停止递归。为了说明这一点,我在做一个数独解算器,在每个自由位置尝试从1到9的每个数字。这个想法是在数独问题解决后终止函数。否则,即使数独被解了,它也会继续运行。@Aristotelis您可能应该在循环中的
exit\u fl=1
之后添加一个
return
,以停止该Zep的循环!错把它拿走了@你能再看一下吗?