2-d interpolation in matlab

6 views (last 30 days)
Prerna Mishra
Prerna Mishra on 16 Oct 2022
Edited: Torsten on 16 Oct 2022
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
Prerna Mishra
Prerna Mishra on 16 Oct 2022
I see. But is there any way to do the problem that I have? Keeping the a dimension constant, can I interpolate the value of v0 for d and k?
John D'Errico
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.

Sign in to comment.

Answers (1)

dpb
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
Prerna Mishra on 16 Oct 2022
Right, I understood that. The problem is that when I interpolate using the code above, I get g which is a scalar. I should have ideally gotten a 7 * 1 vector.
For example in an easier problem say:
v0 = rand(100,3);
kv = 1:100;
g = interp1(kv,v0,1.5,'linear')
The output of interpolation is 3*1.
I was expecting similar for this 3 - dimensional problem. That the output of the interpolation should be na*1 vector and not a scalar.
Torsten
Torsten on 16 Oct 2022
Edited: Torsten 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.

Sign in to comment.

Categories

Find more on Interpolation 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!