-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrad_nonan.m
64 lines (59 loc) · 1.43 KB
/
grad_nonan.m
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
57
58
59
60
61
62
63
64
function [dyE,dxE,dzE] = grad_nonan(E)
%LRX Summary of this function goes here
% Detailed explanation goes here
sz_E=size(E);
dxEr=zeros(sz_E);
dxEr(:)=NaN;
dxEl=dxEr;
dxE=dxEr;
dyEr=dxEr;
dyEl=dxEl;
dyE=dxEr;
dzEr=dxEr;
dzEl=dxEr;
dzE=dxEr;
for x=1:sz_E(1)
for y=1:sz_E(2)
for z=1:sz_E(3)
if(x+1<=sz_E(1))
dxEr(x,y,z)=E(x+1,y,z)-E(x,y,z);
end
if(x-1>=1)
dxEl(x,y,z)=E(x,y,z)-E(x-1,y,z);
end
if(y+1<=sz_E(2))
dyEr(x,y,z)=E(x,y+1,z)-E(x,y,z);
end
if(y-1>=1)
dyEl(x,y,z)=E(x,y,z)-E(x,y-1,z);
end
if(z+1<=sz_E(3))
dzEr(x,y,z)=E(x,y,z+1)-E(x,y,z);
end
if(z-1>=1)
dzEl(x,y,z)=E(x,y,z)-E(x,y,z-1);
end
end
end
end
for x=1:sz_E(1)
for y=1:sz_E(2)
for z=1:sz_E(3)
dxE(x,y,z)=nanchecker(dxEr(x,y,z),dxEl(x,y,z));
dyE(x,y,z)=nanchecker(dyEr(x,y,z),dyEl(x,y,z));
dzE(x,y,z)=nanchecker(dzEr(x,y,z),dzEl(x,y,z));
end
end
end
end
function C=nanchecker(A,B)
if(isnan(A)==1 & isnan(B)==1)
C=NaN;
elseif(isnan(A)==1 & isnan(B)~=1)
C=B;
elseif(isnan(A)~=1 & isnan(B)==1)
C=A;
elseif(isnan(A)~=1 & isnan(B)~=1)
C=0.5*(A+B);
end
end