Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab-递归函数到迭代函数的转换 我知道它在爪哇和C++等中的可能,但是在MATLAB中这是可能的吗? 我最近发现Matlab没有像value++这样的捷径,而是必须使用value=value+1,所以我想知道是否有可能将此函数转换为迭代函数。我不知道从哪里开始。如果是这样的话,它是否比递归函数的好处更小 function [lines] = recurse(R,C, lines, T_l, count, directions) [rows, columns] = size(lines); if((R < 2 || C < 2) || (R > rows-1 || C > columns - 1) || count>500) count= count+1; return; end count= count+1; direction = directions(R,C); if(direction >= 68 || direction <=-68) if(lines(R-1,C) > T_l) lines(R-1,C) = 0; lines = recurse(R-1,C, lines, T_l, count, directions); end if(lines(R+1,C) > T_l) lines(R+1,C) = 0; lines = recurse(R+1,C, lines, T_l, count, directions); end elseif (direction <= -23 && direction >-68) if(lines(R+1,C+1) > T_l) lines(R+1,C+1) = 0; lines = recurse(R+1,C+1, lines, T_l, count, directions); end if(lines(R-1,C-1) > T_l) lines(R-1,C-1) = 0; lines = recurse(R-1,C-1, lines, T_l, count, directions); end elseif (direction >= 23 && direction < 68) if(lines(R+1,C-1) > T_l) lines(R+1,C-1) = 0; lines = recurse(R+1,C-1, lines, T_l, count, directions); end if(lines(R-1,C+1) > T_l) lines(R-1,C+1) = 0; lines = recurse(R-1,C+1, lines, T_l, count, directions); end else if(lines(R,C+1) > T_l) lines(R,C+1) = 0; lines = recurse(R,C+1, lines, T_l, count, directions); end if(lines(R,C-1) > T_l) lines(R,C-1) = 0; lines = recurse(R,C-1, lines, T_l, count, directions); end end lines(R,C) = 255; return; 函数[行]=递归(R、C、行、T\l、计数、方向) [行、列]=大小(行); 如果((R列-1)| |计数>500) 计数=计数+1; 返回; 结束 计数=计数+1; 方向=方向(R,C); 如果(方向>=68 | |方向T|l) 直线(R-1,C)=0; 行=递归(R-1,C,行,T_l,计数,方向); 结束 如果(直线(R+1,C)>T_l) 线(R+1,C)=0; 行=递归(R+1,C,行,T_l,计数,方向); 结束 埃尔塞伊夫(方向-68) 如果(直线(R+1,C+1)>T\U l) 线(R+1,C+1)=0; 行=递归(R+1,C+1,行,T\l,计数,方向); 结束 如果(线路(R-1、C-1)>T_l) 直线(R-1,C-1)=0; 行=递归(R-1,C-1,行,T_l,计数,方向); 结束 elseif(方向>=23和方向T_l) 直线(R+1,C-1)=0; 行=递归(R+1,C-1,行,T_l,计数,方向); 结束 如果(线路(R-1,C+1)>T\U l) 直线(R-1,C+1)=0; 行=递归(R-1,C+1,行,T_l,计数,方向); 结束 其他的 如果(直线(R,C+1)>T\U l) 直线(R,C+1)=0; 行=递归(R,C+1,行,T_l,计数,方向); 结束 如果(直线(R,C-1)>T_l) 直线(R,C-1)=0; 行=递归(R,C-1,行,T_l,计数,方向); 结束 结束 行(R,C)=255; 返回;_Function_Matlab_Loops_Recursion_If Statement - Fatal编程技术网

Matlab-递归函数到迭代函数的转换 我知道它在爪哇和C++等中的可能,但是在MATLAB中这是可能的吗? 我最近发现Matlab没有像value++这样的捷径,而是必须使用value=value+1,所以我想知道是否有可能将此函数转换为迭代函数。我不知道从哪里开始。如果是这样的话,它是否比递归函数的好处更小 function [lines] = recurse(R,C, lines, T_l, count, directions) [rows, columns] = size(lines); if((R < 2 || C < 2) || (R > rows-1 || C > columns - 1) || count>500) count= count+1; return; end count= count+1; direction = directions(R,C); if(direction >= 68 || direction <=-68) if(lines(R-1,C) > T_l) lines(R-1,C) = 0; lines = recurse(R-1,C, lines, T_l, count, directions); end if(lines(R+1,C) > T_l) lines(R+1,C) = 0; lines = recurse(R+1,C, lines, T_l, count, directions); end elseif (direction <= -23 && direction >-68) if(lines(R+1,C+1) > T_l) lines(R+1,C+1) = 0; lines = recurse(R+1,C+1, lines, T_l, count, directions); end if(lines(R-1,C-1) > T_l) lines(R-1,C-1) = 0; lines = recurse(R-1,C-1, lines, T_l, count, directions); end elseif (direction >= 23 && direction < 68) if(lines(R+1,C-1) > T_l) lines(R+1,C-1) = 0; lines = recurse(R+1,C-1, lines, T_l, count, directions); end if(lines(R-1,C+1) > T_l) lines(R-1,C+1) = 0; lines = recurse(R-1,C+1, lines, T_l, count, directions); end else if(lines(R,C+1) > T_l) lines(R,C+1) = 0; lines = recurse(R,C+1, lines, T_l, count, directions); end if(lines(R,C-1) > T_l) lines(R,C-1) = 0; lines = recurse(R,C-1, lines, T_l, count, directions); end end lines(R,C) = 255; return; 函数[行]=递归(R、C、行、T\l、计数、方向) [行、列]=大小(行); 如果((R列-1)| |计数>500) 计数=计数+1; 返回; 结束 计数=计数+1; 方向=方向(R,C); 如果(方向>=68 | |方向T|l) 直线(R-1,C)=0; 行=递归(R-1,C,行,T_l,计数,方向); 结束 如果(直线(R+1,C)>T_l) 线(R+1,C)=0; 行=递归(R+1,C,行,T_l,计数,方向); 结束 埃尔塞伊夫(方向-68) 如果(直线(R+1,C+1)>T\U l) 线(R+1,C+1)=0; 行=递归(R+1,C+1,行,T\l,计数,方向); 结束 如果(线路(R-1、C-1)>T_l) 直线(R-1,C-1)=0; 行=递归(R-1,C-1,行,T_l,计数,方向); 结束 elseif(方向>=23和方向T_l) 直线(R+1,C-1)=0; 行=递归(R+1,C-1,行,T_l,计数,方向); 结束 如果(线路(R-1,C+1)>T\U l) 直线(R-1,C+1)=0; 行=递归(R-1,C+1,行,T_l,计数,方向); 结束 其他的 如果(直线(R,C+1)>T\U l) 直线(R,C+1)=0; 行=递归(R,C+1,行,T_l,计数,方向); 结束 如果(直线(R,C-1)>T_l) 直线(R,C-1)=0; 行=递归(R,C-1,行,T_l,计数,方向); 结束 结束 行(R,C)=255; 返回;

Matlab-递归函数到迭代函数的转换 我知道它在爪哇和C++等中的可能,但是在MATLAB中这是可能的吗? 我最近发现Matlab没有像value++这样的捷径,而是必须使用value=value+1,所以我想知道是否有可能将此函数转换为迭代函数。我不知道从哪里开始。如果是这样的话,它是否比递归函数的好处更小 function [lines] = recurse(R,C, lines, T_l, count, directions) [rows, columns] = size(lines); if((R < 2 || C < 2) || (R > rows-1 || C > columns - 1) || count>500) count= count+1; return; end count= count+1; direction = directions(R,C); if(direction >= 68 || direction <=-68) if(lines(R-1,C) > T_l) lines(R-1,C) = 0; lines = recurse(R-1,C, lines, T_l, count, directions); end if(lines(R+1,C) > T_l) lines(R+1,C) = 0; lines = recurse(R+1,C, lines, T_l, count, directions); end elseif (direction <= -23 && direction >-68) if(lines(R+1,C+1) > T_l) lines(R+1,C+1) = 0; lines = recurse(R+1,C+1, lines, T_l, count, directions); end if(lines(R-1,C-1) > T_l) lines(R-1,C-1) = 0; lines = recurse(R-1,C-1, lines, T_l, count, directions); end elseif (direction >= 23 && direction < 68) if(lines(R+1,C-1) > T_l) lines(R+1,C-1) = 0; lines = recurse(R+1,C-1, lines, T_l, count, directions); end if(lines(R-1,C+1) > T_l) lines(R-1,C+1) = 0; lines = recurse(R-1,C+1, lines, T_l, count, directions); end else if(lines(R,C+1) > T_l) lines(R,C+1) = 0; lines = recurse(R,C+1, lines, T_l, count, directions); end if(lines(R,C-1) > T_l) lines(R,C-1) = 0; lines = recurse(R,C-1, lines, T_l, count, directions); end end lines(R,C) = 255; return; 函数[行]=递归(R、C、行、T\l、计数、方向) [行、列]=大小(行); 如果((R列-1)| |计数>500) 计数=计数+1; 返回; 结束 计数=计数+1; 方向=方向(R,C); 如果(方向>=68 | |方向T|l) 直线(R-1,C)=0; 行=递归(R-1,C,行,T_l,计数,方向); 结束 如果(直线(R+1,C)>T_l) 线(R+1,C)=0; 行=递归(R+1,C,行,T_l,计数,方向); 结束 埃尔塞伊夫(方向-68) 如果(直线(R+1,C+1)>T\U l) 线(R+1,C+1)=0; 行=递归(R+1,C+1,行,T\l,计数,方向); 结束 如果(线路(R-1、C-1)>T_l) 直线(R-1,C-1)=0; 行=递归(R-1,C-1,行,T_l,计数,方向); 结束 elseif(方向>=23和方向T_l) 直线(R+1,C-1)=0; 行=递归(R+1,C-1,行,T_l,计数,方向); 结束 如果(线路(R-1,C+1)>T\U l) 直线(R-1,C+1)=0; 行=递归(R-1,C+1,行,T_l,计数,方向); 结束 其他的 如果(直线(R,C+1)>T\U l) 直线(R,C+1)=0; 行=递归(R,C+1,行,T_l,计数,方向); 结束 如果(直线(R,C-1)>T_l) 直线(R,C-1)=0; 行=递归(R,C-1,行,T_l,计数,方向); 结束 结束 行(R,C)=255; 返回;,function,matlab,loops,recursion,if-statement,Function,Matlab,Loops,Recursion,If Statement,基本上,我现在有一个递归调用第二个函数的函数。我希望将这个递归函数整合到函数中,将其作为一组迭代命令调用。 我很确定它会慢一些,但是速度对我来说不是问题,我很想看看循环是如何工作的。谢谢。您可以使用来完成value++(不过您需要符号工具箱) 运算符(symb,f,T,prio)定义了一个新的运算符符号symb,其类型为T(前缀|后缀|二进制| Nary),优先级为prio。函数f使用新运算符计算表达式 给定运算符符号“+”,例如,使用求值函数f,解析器根据运算符的类型构建以下表达式,其中: 前

基本上,我现在有一个递归调用第二个函数的函数。我希望将这个递归函数整合到函数中,将其作为一组迭代命令调用。
我很确定它会慢一些,但是速度对我来说不是问题,我很想看看循环是如何工作的。谢谢。

您可以使用来完成
value++
(不过您需要符号工具箱)

运算符(symb,f,T,prio)
定义了一个新的运算符符号
symb
,其类型为
T
(前缀|后缀|二进制| Nary),优先级为
prio
。函数
f
使用新运算符计算表达式

给定运算符符号“+”,例如,使用求值函数
f
,解析器根据运算符的类型构建以下表达式,其中:

前缀:输入++x将产生f(x)

后缀:输入x++将产生f(x)

二进制文件:输入x++y++z将产生f(f(x,y),z)

Nary:输入x++y++z将产生f(x,y,z))


更多信息请参见matlab。

1。这在Matlab中是可能的吗?是的。2.如果是这样的话,它是否比递归函数的好处更小?不,不是。这是一种权衡。迭代与递归是算法上的,几乎与语言无关。迭代通常更复杂、更难理解,但在堆栈框架设置等方面产生的开销更少,甚至可能需要更少的内存(尽管这并不总是正确的)。将递归函数转换为迭代函数的一种方法是识别堆栈上存储的内容。看看有多少是绝对必要的。无论需要什么,创建一个类似堆栈的结构来存储它,然后对该结构进行操作。