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:
interp1 - length(x) should equal size(Y,1) error

Subject: interp1 - length(x) should equal size(Y,1) error

From: Jonathan W Smith

Date: 24 Apr, 2013 13:04:09

Message: 1 of 7

Hello

I have the following loop where x, Y, and xi have dimensions of 41, 71, and 7.

for lat = 1:41

     for lon = 1:71

        D = interp1(x(lat,lon, :),Y, xi(lat,lon,:))

     end

end

I get an error that says the length(x) should equal size(Y,1). How can I get these equal when the length of x is 7 and size(Y,1) = 41? Array Y will not mean the same if I make the switch the size of the 1st and 3rd dimension.

Jonathan

Subject: interp1 - length(x) should equal size(Y,1) error

From: Jonathan W Smith

Date: 24 Apr, 2013 13:21:13

Message: 2 of 7

Another error I get is X is not a vector

"Jonathan W Smith" wrote in message <kl8l89$lmd$1@newscl01ah.mathworks.com>...
> Hello
>
> I have the following loop where x, Y, and xi have dimensions of 41, 71, and 7.
>
> for lat = 1:41
>
> for lon = 1:71
>
> D = interp1(x(lat,lon, :),Y, xi(lat,lon,:))
>
> end
>
> end
>
> I get an error that says the length(x) should equal size(Y,1). How can I get these equal when the length of x is 7 and size(Y,1) = 41? Array Y will not mean the same if I make the switch the size of the 1st and 3rd dimension.
>
> Jonathan

Subject: interp1 - length(x) should equal size(Y,1) error

From: Steven_Lord

Date: 24 Apr, 2013 13:34:15

Message: 3 of 7

*moved top-posted follow-up to the bottom*

> "Jonathan W Smith" wrote in message
> <kl8l89$lmd$1@newscl01ah.mathworks.com>...
>> Hello
>>
>> I have the following loop where x, Y, and xi have dimensions of 41, 71,
>> and 7.

So all of:

size(x)
size(Y)
size(xi)

return [41 71 7]?

>> for lat = 1:41
>>
>> for lon = 1:71
>>
>> D = interp1(x(lat,lon, :),Y, xi(lat,lon,:))

Do you expect D to be a 7-by-41-by-71 (or 7-by-71-by-41) array at each
iteration through the loop? If so, try what I describe below. If it is not,
please describe in words (no equations or code) what your want D to be after
this call on each iteration through the nested loops.

You're going to need to convert the first input (your indexing expression
involving x) into a vector (1-by-7 or 7-by-1) using RESHAPE or PERMUTE. You
will then need to convert Y into a 7-by-41-by-71 or a 7-by-71-by-41 using
PERMUTE. Finally convert the third input (the indexing expression involving
xi) into a vector using the same technique as you used for the first input.
Depending on what your end goal is, you may need to IPERMUTE it to get it in
an ordering consistent with your original Y.

*snip*

"Jonathan W Smith" <jwsmith9@gmail.com> wrote in message
news:kl8m89$p88$1@newscl01ah.mathworks.com...
> Another error I get is X is not a vector

In the code you've written above it is not. It is a 1-by-1-by-7 array. As
defined by ISVECTOR:

http://www.mathworks.com/help/matlab/ref/isvector.html

"isvector(A) returns logical 1 (true) if size(A) returns [1 n] or [n 1] with
a nonnegative integer value n, and logical 0 (false) otherwise."

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: interp1 - length(x) should equal size(Y,1) error

From: Jonathan W Smith

Date: 24 Apr, 2013 14:00:10

Message: 4 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <kl8n0n$rm9$1@newscl01ah.mathworks.com>...
> *moved top-posted follow-up to the bottom*
>
> > "Jonathan W Smith" wrote in message
> > <kl8l89$lmd$1@newscl01ah.mathworks.com>...
> >> Hello
> >>
> >> I have the following loop where x, Y, and xi have dimensions of 41, 71,
> >> and 7.
>
> So all of:
>
> size(x)
> size(Y)
> size(xi)
>
> return [41 71 7]?
>
> >> for lat = 1:41
> >>
> >> for lon = 1:71
> >>
> >> D = interp1(x(lat,lon, :),Y, xi(lat,lon,:))
>
> Do you expect D to be a 7-by-41-by-71 (or 7-by-71-by-41) array at each
> iteration through the loop? If so, try what I describe below. If it is not,
> please describe in words (no equations or code) what your want D to be after
> this call on each iteration through the nested loops.
>
> You're going to need to convert the first input (your indexing expression
> involving x) into a vector (1-by-7 or 7-by-1) using RESHAPE or PERMUTE. You
> will then need to convert Y into a 7-by-41-by-71 or a 7-by-71-by-41 using
> PERMUTE. Finally convert the third input (the indexing expression involving
> xi) into a vector using the same technique as you used for the first input.
> Depending on what your end goal is, you may need to IPERMUTE it to get it in
> an ordering consistent with your original Y.
>
> *snip*
>
> "Jonathan W Smith" <jwsmith9@gmail.com> wrote in message
> news:kl8m89$p88$1@newscl01ah.mathworks.com...
> > Another error I get is X is not a vector
>
> In the code you've written above it is not. It is a 1-by-1-by-7 array. As
> defined by ISVECTOR:
>
> http://www.mathworks.com/help/matlab/ref/isvector.html
>
> "isvector(A) returns logical 1 (true) if size(A) returns [1 n] or [n 1] with
> a nonnegative integer value n, and logical 0 (false) otherwise."
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Steve:

Thanks for your response.

x, Y, and xi each have dimensions of 41 by 71 by 7. The 3rd dimension is vertical levels.

D, the interpolated array, should be a 41 by 71 by 7 array.

D is an interpolated array of the 7 vertical levels at each of the 2911 cells. (41x71 = 2911)

Also since x has to be vector, how do I make it x(lat,lon,:) in the nested loop into a vector?

Jonathan

Subject: interp1 - length(x) should equal size(Y,1) error

From: Steven_Lord

Date: 24 Apr, 2013 14:43:52

Message: 5 of 7



"Jonathan W Smith" <jwsmith9@gmail.com> wrote in message
news:kl8oha$3m6$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kl8n0n$rm9$1@newscl01ah.mathworks.com>...

*snip*

> Steve:
>
> Thanks for your response.
>
> x, Y, and xi each have dimensions of 41 by 71 by 7. The 3rd dimension is
> vertical levels.
>
> D, the interpolated array, should be a 41 by 71 by 7 array.
>
> D is an interpolated array of the 7 vertical levels at each of the 2911
> cells. (41x71 = 2911)
> Also since x has to be vector, how do I make it x(lat,lon,:) in the nested
> loop into a vector?

From my message:

>> You're going to need to convert the first input (your indexing expression
>> involving x) into a vector (1-by-7 or 7-by-1) using RESHAPE or PERMUTE.
>> You

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: interp1 - length(x) should equal size(Y,1) error

From: Jonathan W Smith

Date: 24 Apr, 2013 14:59:09

Message: 6 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <kl8r38$c5r$1@newscl01ah.mathworks.com>...
>
>
> "Jonathan W Smith" <jwsmith9@gmail.com> wrote in message
> news:kl8oha$3m6$1@newscl01ah.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <kl8n0n$rm9$1@newscl01ah.mathworks.com>...
>
> *snip*
>
> > Steve:
> >
> > Thanks for your response.
> >
> > x, Y, and xi each have dimensions of 41 by 71 by 7. The 3rd dimension is
> > vertical levels.
> >
> > D, the interpolated array, should be a 41 by 71 by 7 array.
> >
> > D is an interpolated array of the 7 vertical levels at each of the 2911
> > cells. (41x71 = 2911)
> > Also since x has to be vector, how do I make it x(lat,lon,:) in the nested
> > loop into a vector?
>
> From my message:
>
> >> You're going to need to convert the first input (your indexing expression
> >> involving x) into a vector (1-by-7 or 7-by-1) using RESHAPE or PERMUTE.
> >> You
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Steve:

How do I create the 2911 (41 by 71) vectors in x below? Each of the 2911 vectors have the 7 vertical levels. Would I need an additional nested loop?

I thought that the

for lat = 1:41
    for lon = 1:71

         D = interp1(xi(lat,lon,:), Y, x(lat,lon,:));
    end
end

Thanks
Jonathan

Subject: interp1 - length(x) should equal size(Y,1) error

From: Steven_Lord

Date: 24 Apr, 2013 17:10:54

Message: 7 of 7



"Jonathan W Smith" <jwsmith9@gmail.com> wrote in message
news:kl8rvt$f2v$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kl8r38$c5r$1@newscl01ah.mathworks.com>...

*snip*

> How do I create the 2911 (41 by 71) vectors in x below? Each of the 2911
> vectors have the 7 vertical levels. Would I need an additional nested
> loop?
>
> I thought that the
> for lat = 1:41
> for lon = 1:71
>
> D = interp1(xi(lat,lon,:), Y, x(lat,lon,:));
> end
> end
>
> Thanks
> Jonathan

xa = cat(3, 1, 2, 3, 4, 5, 6, 7)
yv = reshape(xa, 1, 7)
yv2 = permute(xa, [2 3 1])
yv3 = reshape(xa, 7, 1)
yv4 = permute(xa, [3 1 2])

All of the yv* variables are vectors with seven elements. Any of them would
work as the first input of an INTERP1 call with a second input that is
7-by-something.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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