Could anyone please explain this matlab code...?

2 次查看(过去 30 天)
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 个)

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