Matlabs函数梯度有必要吗?

发布于 2025-01-09 19:41:25 字数 439 浏览 1 评论 0原文


I stumbled over this by visualizing changes in data. In my opinion the gradient function in Matlab has a weakness for certain inputs. Let's say I have a matrix like a chessfield and compute the gradient:
matrix = repmat([1 5;5 1], 10, 10)
[Fx,Fy] = gradient(matrix);

在矩阵中,我们在两个方向上都有很多变化。但由于渐变的行为,Fx 和 Fy 除边框外仅包含零。
这种行为是想要的吗?那么使用 diff() 然后通过填充实现输入和输出矩阵大小相等不是总是更好吗?或者换句话说,什么时候使用gradient()而不是diff()有用?

I stumbled over this by visualizing changes in data. In my opinion the gradient function in Matlab has a weakness for certain inputs. Let's say I have a matrix like a chessfield and compute the gradient:

matrix = repmat([1 5;5 1], 10, 10)
[Fx,Fy] = gradient(matrix);

In matrix we have a lot of changes in both direction. But because of the behaviour of gradient, Fx and Fy will only contain zeros except for the borders.
Is this behaviour wanted? Is it then not always better to use diff() and then achieving equal size of in- and output matrix with padding? Or in other words, when is it useful to use gradient() instead of diff()?

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

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

发布评论

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

评论(1

千と千尋 2025-01-16 19:41:25

文档告诉我们 gradient 使用中心差异来计算内部点的梯度,这解释了棋盘图案的行为。

该函数用于计算在规则网格上评估的函数的梯度。为了使梯度有意义,我们假设该函数是可微的。但是中心差分(或任何有限差分方案)只有在函数采样足够密集的情况下才有意义,否则您将遇到此类问题。

The documentation tells use that gradient uses central differences to compute the gradient at interior points, which explains the behaviour with your chessboard-pattern.

This function is intended for computing the gradient of a function that was evaluated on a regular grid. For the gradient to make sense, we assume that the function is differentiable. But a central difference (or any finite difference scheme) only makes sense if the function is sampled sufficiently densely, otherwise you will have issues like these.

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