필터 지우기
필터 지우기

Could anyone please explain this matlab code...?

조회 수: 2 (최근 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]);

채택된 답변

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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by