如何在函数中修改数组?

发布于 2024-10-01 14:14:10 字数 114 浏览 8 评论 0原文

MATLAB 是一种按值传递语言。我有一个处理像素邻居的递归函数。每次调用函数时复制图像(在我的例子中是两个图像)的成本非常高。

我使用全局变量来解决这个问题。还有其他方法可以使递归函数修改数组吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦妄 2024-10-08 14:14:10

这里有三个选项,但也许您不需要其中任何一个,因为 Matlab 使用“写时复制”,即只有在修改变量时才会复制变量。

  1. 正如 @gnovice 提到的,您可以使用 < a href="https://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html" rel="nofollow noreferrer">嵌套函数。嵌套函数内部使用的变量在嵌套函数和封闭函数之间共享。嵌套函数的调试有些棘手,并且编写/理解起来有点复杂。
  2. 您可以将图像存储为句柄对象的属性,这是通过引用传递的。
  3. 您可以以不同的方式编写代码,以便不使用递归函数,因为 Matlab 不是使用递归函数的最佳语言。如果您有权访问图像处理工具箱,则可以使用诸如 blockprocim2col 重写该函数。

最后,如果您想保留当前的方案,我强烈建议使用 persistent< /a> 变量而不是全局变量。

You have three options here, but maybe you don't need any of them, since Matlab used 'copy-on-write', i.e. variables are only copied if you modify them.

  1. As @gnovice mentions, you can use a nested function. Variables used inside the nested function are shared between the nested function and the enclosing function. Nested functions are somewhat tricky to debug, and a bit more complicated to write/understand.
  2. You can store your images as properties of a handle object, which is passed by reference.
  3. You can write code differently in order to not use a recursive function, since Matlab isn't the best language for using those. If you have access to the image processing toolbox, you may be able to use functions like 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.

别想她 2024-10-08 14:14:10

MATLAB 并不总是按值传递,较新版本的 MATLAB 在某些情况下会按引用传递,请参阅 就地操作以及有关 MATLAB 内存管理的更一般性讨论 在这篇文章中

如果没有 尾部调用优化,使用递归的效率就会很低,而且 MATLAB 也没有它据我所知,但每个递归都可以转换为循环

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.

荒路情人 2024-10-08 14:14:10

如果您将递归函数设为另一个函数中的嵌套函数,其中存储图像数据,然后递归函数可以修改图像数据

If you make your recursive function a nested function within another function where the image data is stored, then the recursive function can modify the image data without needing to have it passed to it.

转角预定愛 2024-10-08 14:14:10

这是一个常见的误解。虽然MATLAB的sytanx是按值传递的,但它实际上并不像C中那样按值传递。解释器足够聪明,只在必要时才进行复制。因此,您应该继续按值传递,看看是否遇到内存问题。

正如其他发帖者所指出的,无论如何,您都应该尽量避免在 MATLAB 中使用递归。

This is a common misconception. Although the sytanx of MATLAB is pass by value, it does not actually pass by value as in C. The interpreter is smart enough to only make copies when necessary. So you should just go ahead and pass by value and see if you run into memory problems.

As other posters have noted, you should try to avoid recursion in MATLAB anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文