How Gradient is calcuted

In matlab i have found following answers. let X =
1 2 3
3 4 5
3 2 1
z=gradient(X)
z =
1 1 1
1 1 1
-1 -1 -1
[z,c]=gradient(X)
z =
1 1 1
1 1 1
-1 -1 -1
c =
2 2 2
1 0 -1
0 -2 -4
But how z and c is calculated,what z and c indicates.

 採用された回答

Sven
Sven 2013 年 2 月 17 日
編集済み: Sven 2013 年 2 月 17 日

0 投票

Hi Tinkul,
It is probably clearer if you call your variables z and c different names such as dx and dy. This is because the first and second outputs from gradient is the gradient in each of those directions.
So if you have:
M =
1 2 3
3 4 5
3 2 1
[dx,dy]=gradient(M)
dx =
1 1 1
1 1 1
-1 -1 -1
dy =
2 2 2
1 0 -1
0 -2 -4
You'll notice that dx(1,:) is almost the same as diff(M(1,:)), and dy(:,1) is almost the same as diff(M(:,1)). The main difference is that gradient replicates the last result so that the output has the same size as the input.
Update: this is not quite correct. The two-sided gradient is used in gradient... the one-sided gradient is used in diff.
Does that help? You can also check out the help for gradient which says even more.

4 件のコメント

Jan
Jan 2013 年 2 月 17 日
No, gradient does not replicate the last element and it is not almost the same as diff. While the values equal the output of diff at the edges, the two-sided difference quotient is used inside.
Sven
Sven 2013 年 2 月 17 日
Thanks Jan, you're right... I should have just pointed to help gradient :)
Shashank Prasanna
Shashank Prasanna 2013 年 2 月 17 日
Quick note, doc gradient is generally more updated than help gradient or even better is the web documentation search. just an fyi.
Jan
Jan 2013 年 2 月 17 日
The web documentation concerns the newest release only. For the locally installed version, the local help is more accurate, when there have been changes.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2013 年 2 月 17 日

2 投票

Beside help gradient you can read the source code also: edit gradient. There you find, that at the edges the single sided difference quotient is calculated - demonstrated on a vector at first:
dx(1) = (x(2) - x(1)) / h;
dx(3) = (x(3) - x(2)) / h;
while h==1 as default. In the inner points the 2nd order two sided difference quotient is created:
dx(2) = (x(3) - x(1)) / (2*h);
For a matrix input the two out puts are calculated a long the rows and the columns respectively.

1 件のコメント

Tinkul
Tinkul 2013 年 2 月 17 日
Thanks................

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

タグ

質問済み:

2013 年 2 月 17 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by