Matlabs函数梯度有必要吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档告诉我们
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.