Could anyone please explain this matlab code...?

1 回表示 (過去 30 日間)
nivedita
nivedita 2014 年 3 月 20 日
コメント済み: XIA SUN 2016 年 12 月 6 日
function g = NeumannBoundCond(f)
[nrow,ncol] = size(f);
g = f;
g([1 nrow],[1 ncol]) = g([3 nrow-2],[3 ncol-2]);
g([1 nrow],2:end-1) = g([3 nrow-2],2:end-1);
g(2:end-1,[1 ncol]) = g(2:end-1,[3 ncol-2]);
  1 件のコメント
dpb
dpb 2014 年 3 月 20 日
Format code so it's legible...

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

採用された回答

Roger Stafford
Roger Stafford 2014 年 3 月 20 日
The best way to explain your code is to see it in action. Let f be the following:
f =
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
50 51 52 53 54 55 56
Then g will be this:
g =
17 16 17 18 19 20 19
10 9 10 11 12 13 12
17 16 17 18 19 20 19
24 23 24 25 26 27 26
31 30 31 32 33 34 33
38 37 38 39 40 41 40
45 44 45 46 47 48 47
38 37 38 39 40 41 40
As you see, only the outer boundary of the f matrix has been changed. Its values are copies of the rectangle of values two places in from the boundary. The effect is, I suppose, some kind of Neumann-like condition on the outer normal derivative.
Note that this code has one more line than it really needed. It doesn't have to make a special case of the corners. It could have been written equivalently as this:
g([1 nrow],:) = g([3 nrow-2],:);
g(:,[1 ncol]) = g(:,[3 ncol-2]);
  1 件のコメント
XIA SUN
XIA SUN 2016 年 12 月 6 日
It's for some image segmentation related works i suppose.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2014 年 3 月 20 日
In general, the form
array([A B], C)
refers to selecting the elements of the array in which the row is A or B, and the column is any of the values given by C. So (A,C(1)), (A,C(2)), (A,C(3))... (B,C(1)), (B,C(2)), ...
With A, B, C, D all scalar, array([A B], [C D]) would designate four locations, (A,C), (A,D), (B,C), (B,D)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by