<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967</link>
    <title>MATLAB Central Newsreader - How to find vector value via matrix value?</title>
    <description>Feed for thread: How to find vector value via matrix value?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 21 Jul 2008 18:46:03 -0400</pubDate>
      <title>How to find vector value via matrix value?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967#444382</link>
      <author>Ning </author>
      <description>Say I have a 2x3 matrix m and vector v.&lt;br&gt;
m = [1.1,1.7,2.1;...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3.6,3.3,4.2];&lt;br&gt;
v=[0 1 2 3 4]';&lt;br&gt;
If I want to find the nearest value of m in v,in this case, &lt;br&gt;
let the nearest value matrix is nm, then &lt;br&gt;
nm=[1 2 2;...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;4 3 4];&lt;br&gt;
How to find the matrix nm without loop?&lt;br&gt;
Thank you.</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 19:23:40 -0400</pubDate>
      <title>Re: How to find vector value via matrix value?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967#444392</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g62lhb$4sn$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Ning  &amp;lt;ning.robin@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;Say I have a 2x3 matrix m and vector v.&lt;br&gt;
&amp;gt;m = [1.1,1.7,2.1;...&lt;br&gt;
&amp;gt;     3.6,3.3,4.2];&lt;br&gt;
&amp;gt;v=[0 1 2 3 4]';&lt;br&gt;
&amp;gt;If I want to find the nearest value of m in v,in this case, &lt;br&gt;
&amp;gt;let the nearest value matrix is nm, then &lt;br&gt;
&amp;gt;nm=[1 2 2;...&lt;br&gt;
&amp;gt;   4 3 4];&lt;br&gt;
&amp;gt;How to find the matrix nm without loop?&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; interp1(v,v,m,'nearest')&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2     2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     3   NaN&lt;br&gt;
&lt;br&gt;
You can improve this to,&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; interp1(v,v,m,'nearest',max(v))&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2     2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     3     4&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
but if you had a value less than the smallest in v then that value&lt;br&gt;
would be replaced by max(v) rather than min(v). The easiest way to deal&lt;br&gt;
with this is likely:&lt;br&gt;
&lt;br&gt;
m1 = m;&lt;br&gt;
m1(m1 &amp;lt; min(v(:))) = min(v);&lt;br&gt;
m1(m1 &amp;gt; max(v(:))) = max(v);&lt;br&gt;
interp1(v,v,m1,'nearest')&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;I like to build things, I like to do things. I am having&lt;br&gt;
&amp;nbsp;&amp;nbsp;a lot of fun.&quot;                              -- Walter Chrysler</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 20:04:02 -0400</pubDate>
      <title>Re: How to find vector value via matrix value?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967#444402</link>
      <author>Bruno Luong</author>
      <description>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in&lt;br&gt;
message &amp;lt;g62nns$s4g$1@canopus.cc.umanitoba.ca&amp;gt;...&lt;br&gt;
The easiest way to deal&lt;br&gt;
&amp;gt; with this is likely:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; m1 = m;&lt;br&gt;
&amp;gt; m1(m1 &amp;lt; min(v(:))) = min(v);&lt;br&gt;
&amp;gt; m1(m1 &amp;gt; max(v(:))) = max(v);&lt;br&gt;
&amp;gt; interp1(v,v,m1,'nearest')&lt;br&gt;
&amp;gt; -- &lt;br&gt;
&lt;br&gt;
This might be even easier:&lt;br&gt;
&lt;br&gt;
v=unique(v); % precaustion&lt;br&gt;
mn = interp1(v,v,m,'nearest','extrap')&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 20:33:50 -0400</pubDate>
      <title>Re: How to find vector value via matrix value?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967#444417</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g62q3i$nrm$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Bruno Luong &amp;lt;b.luong@fogale.fr&amp;gt; wrote:&lt;br&gt;
&amp;gt;roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in&lt;br&gt;
&amp;gt;message &amp;lt;g62nns$s4g$1@canopus.cc.umanitoba.ca&amp;gt;...&lt;br&gt;
&amp;gt;The easiest way to deal&lt;br&gt;
&amp;gt;&amp;gt; with this is likely:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; m1 = m;&lt;br&gt;
&amp;gt;&amp;gt; m1(m1 &amp;lt; min(v(:))) = min(v);&lt;br&gt;
&amp;gt;&amp;gt; m1(m1 &amp;gt; max(v(:))) = max(v);&lt;br&gt;
&amp;gt;&amp;gt; interp1(v,v,m1,'nearest')&lt;br&gt;
&lt;br&gt;
&amp;gt;This might be even easier:&lt;br&gt;
&lt;br&gt;
&amp;gt;v=unique(v); % precaustion&lt;br&gt;
&amp;gt;mn = interp1(v,v,m,'nearest','extrap')&lt;br&gt;
&lt;br&gt;
Hmmm, that does work for 2007a, but it disagrees with&lt;br&gt;
the documented behaviour:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; help interp1&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;YI = INTERP1(X,Y,XI,METHOD,'extrap') uses the specified method for&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;extrapolation for any elements of XI outside the interval spanned by X.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Alternatively, YI = INTERP1(X,Y,XI,METHOD,EXTRAPVAL) replaces&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;the values outside of the interval spanned by X with EXTRAPVAL.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NaN and 0 are often used for EXTRAPVAL.  The default extrapolation&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;behavior with four input arguments is 'extrap' for 'spline' and 'pchip'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;and EXTRAPVAL = NaN for the other methods.&lt;br&gt;
&lt;br&gt;
On the other hand,&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; doc interp1&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For the 'nearest', 'linear', and 'v5cubic' methods,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;interp1(x,Y,xi,method) returns NaN for any element of xi that&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;is outside the interval spanned by x. For all other methods,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;interp1 performs extrapolation for out of range values.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yi = interp1(x,Y,xi,method,'extrap') uses the specified method&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;to perform extrapolation for out of range values.&lt;br&gt;
&lt;br&gt;
which is consistent with the result actually observed (although&lt;br&gt;
not as clearly written, in my opinion.)&lt;br&gt;
&lt;br&gt;
I'll file a documentation bug.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;Do not wait for leaders. Do it alone, person to person.&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Mother Teresa</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 20:50:17 -0400</pubDate>
      <title>Re: How to find vector value via matrix value?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967#444421</link>
      <author>Bruno Luong</author>
      <description>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in&lt;br&gt;
message &amp;lt;g62rre$4f8$1@canopus.cc.umanitoba.ca&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hmmm, that does work for 2007a, but it disagrees with&lt;br&gt;
&amp;gt; the documented behaviour:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; help interp1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     YI = INTERP1(X,Y,XI,METHOD,'extrap') uses the&lt;br&gt;
specified method for&lt;br&gt;
&amp;gt;     extrapolation for any elements of XI outside the&lt;br&gt;
interval spanned by X.&lt;br&gt;
&amp;gt;     Alternatively, YI = INTERP1(X,Y,XI,METHOD,EXTRAPVAL)&lt;br&gt;
replaces&lt;br&gt;
&amp;gt;     the values outside of the interval spanned by X with&lt;br&gt;
EXTRAPVAL.&lt;br&gt;
&amp;gt;     NaN and 0 are often used for EXTRAPVAL.  The default&lt;br&gt;
extrapolation&lt;br&gt;
&amp;gt;     behavior with four input arguments is 'extrap' for&lt;br&gt;
'spline' and 'pchip'&lt;br&gt;
&amp;gt;     and EXTRAPVAL = NaN for the other methods.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Actually I do agree with the help.&lt;br&gt;
&lt;br&gt;
As I understand, by default, if interp1 is called 4&lt;br&gt;
arguments (no specified extrapolation method), then&lt;br&gt;
- NaN is returned for node that fall outside for 'nearest',&lt;br&gt;
'linear', and 'v5spline'&lt;br&gt;
- real extrapolation for 'spline', 'pchip', 'cubic' (since&lt;br&gt;
'cubic' is special case of 'spline')&lt;br&gt;
&lt;br&gt;
If 'extrap' is specified as fifth argument, then real&lt;br&gt;
extrapolation is performed by applying specified method&lt;br&gt;
(same as interpolation method).&lt;br&gt;
&lt;br&gt;
Bruno </description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 21:27:24 -0400</pubDate>
      <title>Re: How to find vector value via matrix value?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172967#444427</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g62sq9$m02$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Bruno Luong &amp;lt;b.luong@fogale.fr&amp;gt; wrote:&lt;br&gt;
&amp;gt;roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in&lt;br&gt;
&amp;gt;message &amp;lt;g62rre$4f8$1@canopus.cc.umanitoba.ca&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; Hmmm, that does work for 2007a, but it disagrees with&lt;br&gt;
&amp;gt;&amp;gt; the documented behaviour:&lt;br&gt;
&lt;br&gt;
&amp;gt;Actually I do agree with the help.&lt;br&gt;
&lt;br&gt;
I did file the documentation bug report, over the lack of&lt;br&gt;
clarity in the help document. Even after you had (earlier)&lt;br&gt;
shown the actual behaviour, I couldn't see how the help document&lt;br&gt;
allowed it. It wasn't until I was preparing the report itself&lt;br&gt;
that I could finally see how the behaviour was consistant&lt;br&gt;
with the 'help'. Unfortunately the descriptions of&lt;br&gt;
'extrap' and EXTRAPVAL are intermixed, making it relatively&lt;br&gt;
easy to confuse what applies under what circumstances.&lt;br&gt;
&lt;br&gt;
The interp1 'doc' seperates the two parts out better, so&lt;br&gt;
if the 'help' were changed to match the 'doc' then it would&lt;br&gt;
make things clearer.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;When a scientist is ahead of his times, it is often through&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;misunderstanding of current, rather than intuition of future truth.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;In science there is never any error so gross that it won't one day,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;from some perspective, appear prophetic.&quot;  -- Jean Rostand</description>
    </item>
  </channel>
</rss>

