How to modify an array in function?
MATLAB is a pass by value language. I have a recursive function that processes pixel's neighbors. It is very expensive to make the copy of the image (in my case two images) each time the function is called.
I used global variables to solve the problem. Is there any other way to make a recursive function modify an array?
You have three options here, but maybe you don't need any of them, since Matlab used 'copy-on-write', ie variables are only copied if you modify them.
blockproc
, or im2col
to rewrite the function. Finally, if you want to stay with your current scheme, I strongly suggest using persistent variables instead of globals.
MATLAB is not always pass-by-value, newer versions of MATLAB do pass-by-reference under some circumstances, see in-place operations and a more general discussion about MATLAB memory management in this SO post.
Without tail-call optimization it is inefficient to use recursion and MATLAB does not have it as far I know, but every recursion can be transformed into a loop.
如果将递归函数作为存储图像数据的另一个函数中的嵌套函数,则递归函数可以修改图像数据而无需传递给它。
链接地址: http://www.djcxy.com/p/80512.html上一篇: ES6尾递归优化堆栈溢出
下一篇: 如何修改函数中的数组?