2-d interpolation in matlab
Show older comments
I have a matrix v0 nd*nk*na, which depends on values of d, k and a. I want to interpolate the matrix v for values of d and k which are stored in dvec and kvec and range from dmin to dmax and kmin and kmax respectively. I tried to use interp2d as follows:
g = interp2(kvec,dvec,v0,k,d,'linear');
I get this error:
Error using .'
TRANSPOSE does not support N-D arrays. Use PAGETRANSPOSE/PAGECTRANSPOSE to transpose pages or PERMUTE to reorder
dimensions of N-D arrays.
Is there any way to fix this?
5 Comments
Matt J
on 16 Oct 2022
You haven't shown the complete error message (why not?), but it doesn't appear to be coming from interp2.
Prerna Mishra
on 16 Oct 2022
John D'Errico
on 16 Oct 2022
Edited: John D'Errico
on 16 Oct 2022
It appears you are using interp2 in a way that it is not designed to be used. Interp2 allows you to index into a 2 dimensional matrix essentially at intermediate points between elements.
However, v0 is a THREE dimensional matrix. So you would use THREE variables to index into it, yet you want to provide only TWO variables. Do you see the problem?
Prerna Mishra
on 16 Oct 2022
John D'Errico
on 16 Oct 2022
@dpb has given you the correct answer, where I was being too lazy to go. ;-)
Effectively, you need to treat this as a 3-dimensional interpolation. Or you can extract each plane of v0, and then use a 2-d interpolation along that plane, thus for each possible a.
Answers (1)
dpb
on 16 Oct 2022
As per usual, would be far easier to visualize what you're after if would provide a (smallish) sample dataset.
But, if by " Keeping the a dimension constant, ..." you mean interpolating over the other two dimension at a given plane of the 3D array, then sure -- just pass the desired plane into the interp2d call...
ixa=... % a specific plane of 3D array v0 in range 1:size(v0,3)
g = interp2(kvec,dvec,v0(:,:,ixa),k,d,'linear');
Alternatively, coding may be simpler to just use interp3 even if the 3rd dimension interpolated value is constant.
g=interp3(kvec,dvec,avec,v0,k,d,a,'linear');
Nothing says a can't be a single value.
9 Comments
Prerna Mishra
on 16 Oct 2022
Edited: Prerna Mishra
on 16 Oct 2022
Torsten
on 16 Oct 2022
Please run the following code and tell us what MATLAB gives as output concerning the sizes:
size(dvec)
size(kvec)
size(avec)
size(v0)
size(d)
size(k)
size(a0)
g=interp3(dvec,kvec,avec,v0,d,k,a0,'linear');
Prerna Mishra
on 16 Oct 2022
Hmmm....I don't recall I had ever used interp3 without building a meshgrid [X,Y,Z] array, but in keeping with the cartesian coordinate orientation, interp3 is expecting X to be the horizontal and Y to be vertical; hence use
g=interp3(kvec,dvec,avec,v0,d,k,a0,'linear');
instead.
Illustrating with a sample case
M=rand(6,7,3);
X=1:6;Y=1:7;Z=1:3;
interp3(X,Y,Z,M,1,1,1)
So, need to use
interp3(Y,X,Z,M,1,1,1)
instead
Prerna Mishra
on 16 Oct 2022
dpb
on 16 Oct 2022
Without the full error in context we really can't tell because of the excessive use of global where the size mismatch actually is in your user code; the error message you posted is internal.
What it's telling you though, is that somewhere in your code is something like
...
results=[1:3].'; % internal code generates a three-vector somehow
y(1)=results; % user code tries to stuff a vector into a single array element -- BOOM!
If I had to guess, I'd say it's probably
v1(l,i,j) = -valfun_stoch(kd);
where valfun is returning a three vector.
Use the debugger to find your logic/coding error in not maintaining dimensions...use either
dbstop on error
or just set a breakpoint inside valfun and step through it to be sure it does what is expected.
Torsten
on 16 Oct 2022
Well, I told you several times now that the assignment
val = (1/(1 - s))*(c^(1 - s) - 1) + beta*(g*prob(j,:)');
makes "val" a (3x1) vector. But "val" must be a scalar (1x1) for the optimizer to work.
Prerna Mishra
on 16 Oct 2022
Let the result of the interpolation be whatever it is, but in the end, "val" must be a scalar.
If g is a vector of the same size as prob(j,:), g*prob(j,:).' gives a scalar - so this would work.
Since we all don't know what you are doing in your code, we can only point out the mistakes, but cannot give advice on how to change your code adequately.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!