Thread Subject: How to find vector value via matrix value?

Subject: How to find vector value via matrix value?

From: Ning

Date: 21 Jul, 2008 18:46:03

Message: 1 of 6

Say I have a 2x3 matrix m and vector v.
m = [1.1,1.7,2.1;...
     3.6,3.3,4.2];
v=[0 1 2 3 4]';
If I want to find the nearest value of m in v,in this case,
let the nearest value matrix is nm, then
nm=[1 2 2;...
   4 3 4];
How to find the matrix nm without loop?
Thank you.

Subject: How to find vector value via matrix value?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Jul, 2008 19:23:40

Message: 2 of 6

In article <g62lhb$4sn$1@fred.mathworks.com>,
Ning <ning.robin@gmail.com> wrote:
>Say I have a 2x3 matrix m and vector v.
>m = [1.1,1.7,2.1;...
> 3.6,3.3,4.2];
>v=[0 1 2 3 4]';
>If I want to find the nearest value of m in v,in this case,
>let the nearest value matrix is nm, then
>nm=[1 2 2;...
> 4 3 4];
>How to find the matrix nm without loop?

>> interp1(v,v,m,'nearest')

ans =

     1 2 2
     4 3 NaN

You can improve this to,

>> interp1(v,v,m,'nearest',max(v))

ans =

     1 2 2
     4 3 4


but if you had a value less than the smallest in v then that value
would be replaced by max(v) rather than min(v). The easiest way to deal
with this is likely:

m1 = m;
m1(m1 < min(v(:))) = min(v);
m1(m1 > max(v(:))) = max(v);
interp1(v,v,m1,'nearest')
--
  "I like to build things, I like to do things. I am having
  a lot of fun." -- Walter Chrysler

Subject: How to find vector value via matrix value?

From: Bruno Luong

Date: 21 Jul, 2008 20:04:02

Message: 3 of 6

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g62nns$s4g$1@canopus.cc.umanitoba.ca>...
The easiest way to deal
> with this is likely:
>
> m1 = m;
> m1(m1 < min(v(:))) = min(v);
> m1(m1 > max(v(:))) = max(v);
> interp1(v,v,m1,'nearest')
> --

This might be even easier:

v=unique(v); % precaustion
mn = interp1(v,v,m,'nearest','extrap')

% Bruno

Subject: How to find vector value via matrix value?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Jul, 2008 20:33:50

Message: 4 of 6

In article <g62q3i$nrm$1@fred.mathworks.com>,
Bruno Luong <b.luong@fogale.fr> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <g62nns$s4g$1@canopus.cc.umanitoba.ca>...
>The easiest way to deal
>> with this is likely:

>> m1 = m;
>> m1(m1 < min(v(:))) = min(v);
>> m1(m1 > max(v(:))) = max(v);
>> interp1(v,v,m1,'nearest')

>This might be even easier:

>v=unique(v); % precaustion
>mn = interp1(v,v,m,'nearest','extrap')

Hmmm, that does work for 2007a, but it disagrees with
the documented behaviour:

>> help interp1

    YI = INTERP1(X,Y,XI,METHOD,'extrap') uses the specified method for
    extrapolation for any elements of XI outside the interval spanned by X.
    Alternatively, YI = INTERP1(X,Y,XI,METHOD,EXTRAPVAL) replaces
    the values outside of the interval spanned by X with EXTRAPVAL.
    NaN and 0 are often used for EXTRAPVAL. The default extrapolation
    behavior with four input arguments is 'extrap' for 'spline' and 'pchip'
    and EXTRAPVAL = NaN for the other methods.

On the other hand,

>> doc interp1

    For the 'nearest', 'linear', and 'v5cubic' methods,
    interp1(x,Y,xi,method) returns NaN for any element of xi that
    is outside the interval spanned by x. For all other methods,
    interp1 performs extrapolation for out of range values.

    yi = interp1(x,Y,xi,method,'extrap') uses the specified method
    to perform extrapolation for out of range values.

which is consistent with the result actually observed (although
not as clearly written, in my opinion.)

I'll file a documentation bug.
--
  "Do not wait for leaders. Do it alone, person to person."
                                              -- Mother Teresa

Subject: How to find vector value via matrix value?

From: Bruno Luong

Date: 21 Jul, 2008 20:50:17

Message: 5 of 6

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g62rre$4f8$1@canopus.cc.umanitoba.ca>...

>
> Hmmm, that does work for 2007a, but it disagrees with
> the documented behaviour:
>
> >> help interp1
>
> YI = INTERP1(X,Y,XI,METHOD,'extrap') uses the
specified method for
> extrapolation for any elements of XI outside the
interval spanned by X.
> Alternatively, YI = INTERP1(X,Y,XI,METHOD,EXTRAPVAL)
replaces
> the values outside of the interval spanned by X with
EXTRAPVAL.
> NaN and 0 are often used for EXTRAPVAL. The default
extrapolation
> behavior with four input arguments is 'extrap' for
'spline' and 'pchip'
> and EXTRAPVAL = NaN for the other methods.
>

Actually I do agree with the help.

As I understand, by default, if interp1 is called 4
arguments (no specified extrapolation method), then
- NaN is returned for node that fall outside for 'nearest',
'linear', and 'v5spline'
- real extrapolation for 'spline', 'pchip', 'cubic' (since
'cubic' is special case of 'spline')

If 'extrap' is specified as fifth argument, then real
extrapolation is performed by applying specified method
(same as interpolation method).

Bruno

Subject: How to find vector value via matrix value?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Jul, 2008 21:27:24

Message: 6 of 6

In article <g62sq9$m02$1@fred.mathworks.com>,
Bruno Luong <b.luong@fogale.fr> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <g62rre$4f8$1@canopus.cc.umanitoba.ca>...

>> Hmmm, that does work for 2007a, but it disagrees with
>> the documented behaviour:

>Actually I do agree with the help.

I did file the documentation bug report, over the lack of
clarity in the help document. Even after you had (earlier)
shown the actual behaviour, I couldn't see how the help document
allowed it. It wasn't until I was preparing the report itself
that I could finally see how the behaviour was consistant
with the 'help'. Unfortunately the descriptions of
'extrap' and EXTRAPVAL are intermixed, making it relatively
easy to confuse what applies under what circumstances.

The interp1 'doc' seperates the two parts out better, so
if the 'help' were changed to match the 'doc' then it would
make things clearer.
--
  "When a scientist is ahead of his times, it is often through
   misunderstanding of current, rather than intuition of future truth.
   In science there is never any error so gross that it won't one day,
   from some perspective, appear prophetic." -- Jean Rostand

Tags for this Thread

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.

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