Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
function gradient.

Subject: function gradient.

From: zhang

Date: 17 May, 2010 15:25:07

Message: 1 of 4

I want to use function gradient to compute a matrix.
For example:
x = [2 4 5; 5 6 9; 0 1 3];
[fx, fy] = gradient(x);

fx and fy are both 3*3 matix.

My question is how to get a 3*3 result? It seems that we can only
get a 2*2 result?

Thanks in advanced

Subject: function gradient.

From: Walter Roberson

Date: 17 May, 2010 15:51:34

Message: 2 of 4

zhang wrote:
> I want to use function gradient to compute a matrix.
> For example:
> x = [2 4 5; 5 6 9; 0 1 3];
> [fx, fy] = gradient(x);
>
> fx and fy are both 3*3 matix.
>
> My question is how to get a 3*3 result? It seems that we can only
> get a 2*2 result?

What value do you expect at each of the 3 x 3 locations? In one
direction, the difference between adjacent elements would give you a 2 x
3 matrix, and in the other direction the difference between adjacent
elements would give you a 3 x 2 matrix, so there is only a 2 x 2 overlap
where the slope is defined in both the x and y direction.

Subject: function gradient.

From: Roger Stafford

Date: 17 May, 2010 17:29:04

Message: 3 of 4

"zhang " <xiaoc10@gmail.com> wrote in message <hsrn4j$o85$1@fred.mathworks.com>...
> I want to use function gradient to compute a matrix.
> For example:
> x = [2 4 5; 5 6 9; 0 1 3];
> [fx, fy] = gradient(x);
>
> fx and fy are both 3*3 matix.
>
> My question is how to get a 3*3 result? It seems that we can only
> get a 2*2 result?
>
> Thanks in advanced

  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.

  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

fx(i,j) = (f(i,j+1)-f(i,j-1))/(hx(j+1)-hx(j-1))

instead of

fx(i,j) =
(f(i,j+1)-f(i,j))/(hx(j+1)-hx(j)) * (hx(j)-hx(j-1))/(hx(j+1)-hx(j-1) +
(f(i,j)-f(i,j-1))/(hx(j)-hx(j-1)) * (hx(j+1)-hx(j))/(hx(j+1)-hx(j-1)

which is the appropriate second order approximation for varying length intervals in x.

Roger Stafford

Subject: function gradient.

From: Bruno Luong

Date: 17 May, 2010 18:20:22

Message: 4 of 4

"zhang " <xiaoc10@gmail.com> wrote in message <hsrn4j$o85$1@fred.mathworks.com>...
> I want to use function gradient to compute a matrix.
> For example:
> x = [2 4 5; 5 6 9; 0 1 3];
> [fx, fy] = gradient(x);
>
> fx and fy are both 3*3 matix.
>
> My question is how to get a 3*3 result? It seems that we can only
> get a 2*2 result?

Odd, I *do* get (3 x 3) matrix

>> x = [2 4 5; 5 6 9; 0 1 3]

x =

     2 4 5
     5 6 9
     0 1 3

>> [fx, fy] = gradient(x)

fx =

    2.0000 1.5000 1.0000
    1.0000 2.0000 3.0000
    1.0000 1.5000 2.0000


fy =

    3.0000 2.0000 4.0000
   -1.0000 -1.5000 -1.0000
   -5.0000 -5.0000 -6.0000

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us