Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
interp3 interpolation

Subject: interp3 interpolation

From: Aditya Gupta

Date: 5 Dec, 2009 01:04:07

Message: 1 of 11

Hello, I've two layers at (x,y, z = 1) and (x,y,z = 2) and I'm trying to interpolate to have two additional layers in between these two layers. The two additional layers should have values which is an interpolation (spline or cubic) of the original two layers. I'm working with interp3 but getting the error below:

%slice is a 613 X 561 X 2 of type double
m1 = size(slice,1);
    n1 = size(slice,2);
    X1 = 1:m1;
    Y1 = 1:n1;
    Z1 = 1:2;
    V = slice(:,:,1:2);
    
    XI = 1:m1;
    YI = 1:n1;
    ZI = [1.3,1.7];
    
    VI = interp3(X1,Y1,Z1,V,XI,YI,ZI);

The error I'm getting is:
Error using ==> interp3 at 129
The lengths of X,Y and Z must match the size of V.

Error in ==> hrt3dconstinter1 at 266
    VI = interp3(X1,Y1,Z1,V,XI,YI,ZI);

>>size(V)

ans =

   613 561 2

I don't understand, the size(V) is supposed to be [m1,n1,2] according to matlab doc.

Can you please help me what mistake am I making? Thank you.

Gupta

Subject: interp3 interpolation

From: Bruno Luong

Date: 5 Dec, 2009 12:43:05

Message: 2 of 11

Not check on Matlab, but I'm pretty sure the inputs XI, YI, ZI should have the same size, because they are usually scattered data. It is not the case in your code

Bruno

Subject: interp3 interpolation

From: Bruno Luong

Date: 5 Dec, 2009 13:20:19

Message: 3 of 11

BTW, INTERP3 is overkill, INTERP1 is enough

Bruno

Subject: interp3 interpolation

From: Aditya Gupta

Date: 5 Dec, 2009 20:10:18

Message: 4 of 11

Thanks for the reply. The MATLAB doc says
X=1:N, Y=1:M, Z=1:P where [M,N,P]=size(V).
Regarding the XI, YI and ZI MATLAB doc says:
XI,YI, ZI must be arrays of the same size, or vectors. Vector arguments that are not the same size, and have mixed orientations (i.e. with both row and column vectors) are passed through meshgrid to create the Y1, Y2, Y3 arrays.

I'm not clear on the last statement. Anyway can you suggest a method I can use to obtain the intensities at the two intermediate layers (as mentioned in the initial post). Is there any other function I could use?

Thanks.

Gupt



"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hfdmmj$ql5$1@fred.mathworks.com>...
> BTW, INTERP3 is overkill, INTERP1 is enough
>
> Bruno

Subject: interp3 interpolation

From: Bruno Luong

Date: 5 Dec, 2009 20:34:20

Message: 5 of 11

"Aditya Gupta" <adityargupta@gmail.com> wrote in message <hfeena$fhb$1@fred.mathworks.com>...
> Thanks for the reply. The MATLAB doc says
> X=1:N, Y=1:M, Z=1:P where [M,N,P]=size(V).
> Regarding the XI, YI and ZI MATLAB doc says:
> XI,YI, ZI must be arrays of the same size, or vectors. Vector arguments that are not the same size, and have mixed orientations (i.e. with both row and column vectors) are passed through meshgrid to create the Y1, Y2, Y3 arrays.
>

Mix orientation: an example
XI = 1:3
YI = (4:5).'
ZI = 6:7

There are two rows vectors and one column vector. In your case they are all rows. All rows/columns are interpreted as a long list of scattered data, so they must have the same length.

Just flip one then, or generate same size array with ndgrid.

Bruno

Subject: interp3 interpolation

From: Adam Aitkenhead

Date: 9 Dec, 2009 13:54:02

Message: 6 of 11

Try changing the order of the inputs to interp3 from...

> VI = interp3(X1,Y1,Z1,V,XI,YI,ZI);

...to...

> VI = interp3(Y1,X1,Z1,V,XI,YI,ZI);

This seems to work for the example you give, as well as for some other test cases I have tried (in Matlab R14).

Unless I have misunderstood it (which is quite possible!), this problem seems to be a result of an error in the documention for interp3.

Adam

Subject: interp3 interpolation

From: Adam Aitkenhead

Date: 9 Dec, 2009 16:32:21

Message: 7 of 11

In fact the order of the XI,YI,ZI needs to be changed as well. If you change the line ...

VI = interp3(X1,Y1,Z1,V,XI,YI,ZI);

 ... to ...

VI = interp3(Y1,X1,Z1,V,YI,XI,ZI);

... then your code should work.

Adam

Subject: interp3 interpolation

From: Sadik

Date: 27 Aug, 2010 20:15:29

Message: 8 of 11

Very nice Adam, thank you.
I hope mathworks fixes that...

Subject: interp3 interpolation

From: Matt J

Date: 27 Aug, 2010 22:05:22

Message: 9 of 11

"Adam Aitkenhead" <adamaitkenheadNOSPAM@NOSPAMhotmail.com> wrote in message <hfoa5q$5or$1@fred.mathworks.com>...
> Try changing the order of the inputs to interp3 from...
>
> > VI = interp3(X1,Y1,Z1,V,XI,YI,ZI);
>
> ...to...
>
> > VI = interp3(Y1,X1,Z1,V,XI,YI,ZI);
>
> This seems to work for the example you give, as well as for some other test cases I have tried (in Matlab R14).
>
> Unless I have misunderstood it (which is quite possible!), this problem seems to be a result of an error in the documention for interp3.
================

No, there's no error in the documentation. If you read closely, you'll see that interp3 expects input data conforming with meshgrid.

However, I agree that it is a regrettable convention. The convention is meant to accomodate people who insist on thinking of the X-axis as horizontal. Because in matrix
indexing M(i,j) the j-axis is usually thought to be horizontal, you have to associate XI coordinate data with j-coordinates, rather than i-coordinates, in order for things to conform.

Incidentally, interpn doesn't obey this convention.

Subject: interp3 interpolation

From: Sadik

Date: 27 Aug, 2010 22:30:26

Message: 10 of 11

Thanks for the clarification Matt.

I think they should give at least an example that defines the column vectors x, y and z and shows how meshgrid and the ordering of the dimensions fits into the whole picture.

In general, the examples in the documentation are very helpful, but the interp3 documentation is surprisingly quite poor.

Best.

Subject: interp3 interpolation

From: tudor dima

Date: 7 Jul, 2011 17:11:11

Message: 11 of 11

it is an error by omission, since in the MESHGRID doc one finds this illuminating bit :-)

 MESHGRID is like NDGRID except that the order of the first two input
    and output arguments are __switched__ (i.e., [X,Y,Z] = MESHGRID(x,y,z)
    produces the same result as [Y,X,Z] = NDGRID(y,x,z))

To add to the confusion, in the INTERP2 help one finds:
ZI = INTERP2(Z,XI,YI) assumes X=1:N and Y=1:M where [M,N]=SIZE(Z).
so one has to swap X and Y only when calling it with MESHGRID outputs...

I agree, it is a regretable convention, almost everybody uses the matrix convention
M(i,j) with the j-axis taken to be horizontal (for instance in all image functions)

Thanks, Adam and Matt!

Tudor


"Matt J" wrote in message <i59cr2$eom$1@fred.mathworks.com>...
>
> No, there's no error in the documentation. If you read closely, you'll see that interp3 expects input data conforming with meshgrid.
>
> However, I agree that it is a regrettable convention. The convention is meant to accomodate people who insist on thinking of the X-axis as horizontal. Because in matrix
> indexing M(i,j) the j-axis is usually thought to be horizontal, you have to associate XI coordinate data with j-coordinates, rather than i-coordinates, in order for things to conform.
>
> Incidentally, interpn doesn't obey this convention.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us