http://www.mathworks.com/matlabcentral/newsreader/view_thread/282372
MATLAB Central Newsreader  function gradient.
Feed for thread: function gradient.
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 17 May 2010 15:25:07 +0000
function gradient.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282372#746034
zhang
I want to use function gradient to compute a matrix.<br>
For example:<br>
x = [2 4 5; 5 6 9; 0 1 3];<br>
[fx, fy] = gradient(x);<br>
<br>
fx and fy are both 3*3 matix.<br>
<br>
My question is how to get a 3*3 result? It seems that we can only<br>
get a 2*2 result?<br>
<br>
Thanks in advanced

Mon, 17 May 2010 15:51:34 +0000
Re: function gradient.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282372#746039
Walter Roberson
zhang wrote:<br>
> I want to use function gradient to compute a matrix.<br>
> For example:<br>
> x = [2 4 5; 5 6 9; 0 1 3];<br>
> [fx, fy] = gradient(x);<br>
> <br>
> fx and fy are both 3*3 matix.<br>
> <br>
> My question is how to get a 3*3 result? It seems that we can only<br>
> get a 2*2 result?<br>
<br>
What value do you expect at each of the 3 x 3 locations? In one <br>
direction, the difference between adjacent elements would give you a 2 x <br>
3 matrix, and in the other direction the difference between adjacent <br>
elements would give you a 3 x 2 matrix, so there is only a 2 x 2 overlap <br>
where the slope is defined in both the x and y direction.

Mon, 17 May 2010 17:29:04 +0000
Re: function gradient.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282372#746075
Roger Stafford
"zhang " <xiaoc10@gmail.com> wrote in message <hsrn4j$o85$1@fred.mathworks.com>...<br>
> I want to use function gradient to compute a matrix.<br>
> For example:<br>
> x = [2 4 5; 5 6 9; 0 1 3];<br>
> [fx, fy] = gradient(x);<br>
> <br>
> fx and fy are both 3*3 matix.<br>
> <br>
> My question is how to get a 3*3 result? It seems that we can only<br>
> get a 2*2 result?<br>
> <br>
> Thanks in advanced<br>
<br>
With [fx,fy] = gradient(f), at the edge points of fx and fy, 'gradient' does a 'diff' of f with the next inside point, but in the interior it takes the difference of the two f values on either side, divided by 2. This way the fx and fy matrices are the same size as f. However, this can make edge gradient values somewhat skewed towards inner values.<br>
<br>
Also note that with [fx,fy] = gradient(f,hx,hy), if hx and hy are vectors containing varying intervals, 'gradient' does not achieve second order approximations to gradient values. That is, for example, on interior points it uses the simpler<br>
<br>
fx(i,j) = (f(i,j+1)f(i,j1))/(hx(j+1)hx(j1))<br>
<br>
instead of<br>
<br>
fx(i,j) = <br>
(f(i,j+1)f(i,j))/(hx(j+1)hx(j)) * (hx(j)hx(j1))/(hx(j+1)hx(j1) +<br>
(f(i,j)f(i,j1))/(hx(j)hx(j1)) * (hx(j+1)hx(j))/(hx(j+1)hx(j1)<br>
<br>
which is the appropriate second order approximation for varying length intervals in x.<br>
<br>
Roger Stafford

Mon, 17 May 2010 18:20:22 +0000
Re: function gradient.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282372#746110
Bruno Luong
"zhang " <xiaoc10@gmail.com> wrote in message <hsrn4j$o85$1@fred.mathworks.com>...<br>
> I want to use function gradient to compute a matrix.<br>
> For example:<br>
> x = [2 4 5; 5 6 9; 0 1 3];<br>
> [fx, fy] = gradient(x);<br>
> <br>
> fx and fy are both 3*3 matix.<br>
> <br>
> My question is how to get a 3*3 result? It seems that we can only<br>
> get a 2*2 result?<br>
<br>
Odd, I *do* get (3 x 3) matrix<br>
<br>
>> x = [2 4 5; 5 6 9; 0 1 3]<br>
<br>
x =<br>
<br>
2 4 5<br>
5 6 9<br>
0 1 3<br>
<br>
>> [fx, fy] = gradient(x)<br>
<br>
fx =<br>
<br>
2.0000 1.5000 1.0000<br>
1.0000 2.0000 3.0000<br>
1.0000 1.5000 2.0000<br>
<br>
<br>
fy =<br>
<br>
3.0000 2.0000 4.0000<br>
1.0000 1.5000 1.0000<br>
5.0000 5.0000 6.0000<br>
<br>
Bruno