Thread Subject: interp1 issue

Subject: interp1 issue

From: Ning

Date: 13 Nov, 2008 06:28:02

Message: 1 of 4

For the example in matlab help 'interp1',
t = 1900:10:1990;
p = [75.995 91.972 105.711 123.203 131.669...
     150.697 179.323 203.212 226.505 249.633];
year=1975;
interp1(t,p,year)
'ans =214.8585'

What if the t is a 3-d matrix (2x3x10), p remains constant, and year is a 2-d matrix (2x3), the answer would be a 2-d matrix (2x3) with various p values. How to do this in matlab?

Subject: interp1 issue

From: Titus

Date: 13 Nov, 2008 10:39:34

Message: 2 of 4


"Ning" <ning.robin@gmail.com> schrieb im Newsbeitrag
news:gfghdh$405$1@fred.mathworks.com...
> For the example in matlab help 'interp1',
> t = 1900:10:1990;
> p = [75.995 91.972 105.711 123.203 131.669...
> 150.697 179.323 203.212 226.505 249.633];
> year=1975;
> interp1(t,p,year)
> 'ans =214.8585'
>
> What if the t is a 3-d matrix (2x3x10), p remains constant, and year is a
> 2-d matrix (2x3), the answer would be a 2-d matrix (2x3) with various p
> values. How to do this in matlab?


Hi,
as long as your data are not too large, I guess the simplest approach would
be a loop here:

res = zeros(size(year));
for i=1:size(res,1)
  for j=1:size(res,2)
    res(i,j) = interp1(t(i,j,:), p, year(i,j));
  end
end

Although: interesting, the t changes but the values are the same? How come?

Titus

Subject: interp1 issue

From: Ning

Date: 13 Nov, 2008 11:44:02

Message: 3 of 4

Thank you, Titus. Here's the for loop code.
t = randn(2,3,5);
p = [10:10:50];
year=randn(2,3);
for row=1:2
    for col=1:3
        out(row,col)=interp1(squeeze(t(row,col,:)), p,year(row,col),'linear','extrap')
    end
end
But I can't use the for loop, since the 't' may be lare, like randn(600,500,5).
Any idea about this without loop?

"Titus" <titus.edelhofer@mathworks.de> wrote in message <gfh057$fbt$1@fred.mathworks.com>...
>
> "Ning" <ning.robin@gmail.com> schrieb im Newsbeitrag
> news:gfghdh$405$1@fred.mathworks.com...
> > For the example in matlab help 'interp1',
> > t = 1900:10:1990;
> > p = [75.995 91.972 105.711 123.203 131.669...
> > 150.697 179.323 203.212 226.505 249.633];
> > year=1975;
> > interp1(t,p,year)
> > 'ans =214.8585'
> >
> > What if the t is a 3-d matrix (2x3x10), p remains constant, and year is a
> > 2-d matrix (2x3), the answer would be a 2-d matrix (2x3) with various p
> > values. How to do this in matlab?
>
>
> Hi,
> as long as your data are not too large, I guess the simplest approach would
> be a loop here:
>
> res = zeros(size(year));
> for i=1:size(res,1)
> for j=1:size(res,2)
> res(i,j) = interp1(t(i,j,:), p, year(i,j));
> end
> end
>
> Although: interesting, the t changes but the values are the same? How come?
>
> Titus
>

Subject: interp1 issue

From: John D'Errico

Date: 13 Nov, 2008 12:32:01

Message: 4 of 4

"Ning" <ning.robin@gmail.com> wrote in message <gfh3u2$ol1$1@fred.mathworks.com>...
> Thank you, Titus. Here's the for loop code.
> t = randn(2,3,5);
> p = [10:10:50];
> year=randn(2,3);
> for row=1:2
> for col=1:3
> out(row,col)=interp1(squeeze(t(row,col,:)), p,year(row,col),'linear','extrap')
> end
> end
> But I can't use the for loop, since the 't' may be lare, like randn(600,500,5).
> Any idea about this without loop?

Sorry, but you need a loop if the t variable varies.

Some things are not easily vectorizable. This is one
of them, unless you choose to rewrite interp1. The
real problem lies in the use of histc to efficiently
assign your points into bins for the interpolation.
If the third dimension of t is small for your problem,
it would be possible to write that part to be efficient
without the use of histc. For that to work, t must be
monotonic in the third dimension, which it is not in
your random data.

John

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
interp1 matrix Ning 13 Nov, 2008 01:30:21
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com