Unexpected result with griddedInterpolant

I'm using Matlab R2011b.
My question is, given a griddedInterpolant F, with underlying values f, and two same sized matricies r and c, (which are not necessarily the same size as f), how do I get a new matrix X, that is the same as:
X = zeros(size(r));
for i=1:size(r,1)
for j=1:size(r,2)
X(i,j) = F(r(i,j),c(i,j));
end
end
Here is a minimum example that demonstrates what I've tried so far
>> f = magic(5);
>> f(5,:) = [];
>> f
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
>> F = griddedInterpolant(f);
>> [c,r] = meshgrid(1:5,1:4);
>> r
r =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
>> c
c =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
>> F(r,c)
The last line gives the same as f, as I would expect. That is, r and c are a pair of matricies specifying the coordinates in each dimension that I'm trying to evaluate.
However, when I try this
>> X = F(r+0.5,c)
20.0000 NaN NaN NaN NaN
13.5000 NaN NaN NaN NaN
7.0000 NaN NaN NaN NaN
NaN NaN NaN NaN NaN
I had expected X(4,:) = NaN and for all other elements,
X(i,j) = (X(i,j) + X(i+1,j)) / 2
Instead, I get that for the first column, and the rest are NaN. I assume that Matlab is interpreting the evaluation as number (2) listed at end of question, instead of (3).
When I try F({r,c}), I get this error.
Error using griddedInterpolant/subsref
Grid vector is not properly defined.
From the below documentation, I would of expected that
when size(r) == size(f), X = F(r,c)
otherwise X = F({r,c})
but this appears not to be the case.
Documentation:
Evaluation
The interpolant F can be evaluated on scattered or gridded query locations as follows:
1) Vq = F(Xq) evaluates F at the query locations Xq to produce the corresponding values Vq. The m-by-n matrix Xq represents m points scattered in n-dimensional space. Vq is m-by-1.
2) Vq = F(xq1,xq2,...,xqn) specifies the query locations xqi as column vectors of length m representing m points scattered in n-dimensional space. Vq is m-by-1.
3) Vq = F(Xq1,Xq2,...,Xqn) specifies the query locations as n n-dimensional arrays Xqi of equal size specifying the full grid. Vq is the same size as Xqi.
4) Vq = F({xgq1,xgq2,...,xgqn}) specifies the query locations as a grid formed by the grid vectors xgqi. Vq is the size of grid defined by xgqi, length(xgq1)-by-length(xgq2)-by- . . . -by-length(xgqn).

Réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by