Mercurial > hg > octave-lyh
comparison scripts/general/interpn.m @ 10427:62bb59f927b1
scripts/general/interp2.m, scripts/general/interpn.m: For nearest neighbour interpolation ceil (instead of floor) at the center of the data intervals to be compatible with Matlab. Add test.
author | Soren Hauberg <hauberg@gmail.com> |
---|---|
date | Sat, 20 Mar 2010 14:26:03 -0700 |
parents | 16f53d29049f |
children | 95c3e38098bf |
comparison
equal
deleted
inserted
replaced
10426:4db7beace28e | 10427:62bb59f927b1 |
---|---|
147 y{i} = y{i}(:); | 147 y{i} = y{i}(:); |
148 yidx{i} = lookup (x{i}, y{i}, "lr"); | 148 yidx{i} = lookup (x{i}, y{i}, "lr"); |
149 endfor | 149 endfor |
150 idx = cell (1,nd); | 150 idx = cell (1,nd); |
151 for i = 1 : nd | 151 for i = 1 : nd |
152 idx{i} = yidx{i} + (y{i} - x{i}(yidx{i}) > x{i}(yidx{i} + 1) - y{i}); | 152 idx{i} = yidx{i} + (y{i} - x{i}(yidx{i}) >= x{i}(yidx{i} + 1) - y{i}); |
153 endfor | 153 endfor |
154 vi = v (sub2ind (sz, idx{:})); | 154 vi = v (sub2ind (sz, idx{:})); |
155 idx = zeros (prod(yshape),1); | 155 idx = zeros (prod(yshape),1); |
156 for i = 1 : nd | 156 for i = 1 : nd |
157 idx |= y{i} < min (x{i}(:)) | y{i} > max (x{i}(:)); | 157 idx |= y{i} < min (x{i}(:)) | y{i} > max (x{i}(:)); |
254 | 254 |
255 %!test | 255 %!test |
256 %! [x,y,z] = ndgrid(0:2); | 256 %! [x,y,z] = ndgrid(0:2); |
257 %! f = x.^2+y.^2+z.^2; | 257 %! f = x.^2+y.^2+z.^2; |
258 %! assert (interpn(x,y,-z,f,1.5,1.5,-1.5), 7.5) | 258 %! assert (interpn(x,y,-z,f,1.5,1.5,-1.5), 7.5) |
259 | |
260 %!test % for Matlab-compatible rounding for 'nearest' | |
261 %! X = meshgrid (1:4); | |
262 %! assert (interpn (X, 2.5, 2.5, 'nearest'), 3); | |
263 |