<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467</link>
    <title>MATLAB Central Newsreader - how to make the following case vectorized?</title>
    <description>Feed for thread: how to make the following case vectorized?</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>Wed, 04 Jun 2008 19:34:03 -0400</pubDate>
      <title>how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#435876</link>
      <author>James Anderson</author>
      <description>Hi, &lt;br&gt;
&lt;br&gt;
I have a matrix (say 10000 rows and 50 columns). I want to&lt;br&gt;
find the rank for each entry within the row it belongs to. I&lt;br&gt;
know how to do this by using a loop, suppose A is the matrix&lt;br&gt;
&lt;br&gt;
for k = 1:10000;&lt;br&gt;
&amp;nbsp;&amp;nbsp;[sortx,ind] = sort(A(k,:));&lt;br&gt;
&amp;nbsp;&amp;nbsp;rankt(ind) = 1:50;&lt;br&gt;
&amp;nbsp;&amp;nbsp;Rank(k,:) = rankt;&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
The Rank contains what I need. However, this is going to&lt;br&gt;
take a long time, especially when the number of rows get&lt;br&gt;
even larger. I found the function sort can sort the whole&lt;br&gt;
matrix along one direction, however, is there any vectorized&lt;br&gt;
way to get the Rank very fast without doing any looping? &lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
James&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;</description>
    </item>
    <item>
      <pubDate>Wed, 04 Jun 2008 20:02:08 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#435881</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g26qnb$4nt$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
James Anderson &amp;lt;janderson_net@yahoo.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;I have a matrix (say 10000 rows and 50 columns). I want to&lt;br&gt;
&amp;gt;find the rank for each entry within the row it belongs to. I&lt;br&gt;
&amp;gt;know how to do this by using a loop, suppose A is the matrix&lt;br&gt;
&lt;br&gt;
&amp;gt;for k = 1:10000;&lt;br&gt;
&amp;gt;  [sortx,ind] = sort(A(k,:));&lt;br&gt;
&amp;gt;  rankt(ind) = 1:50;&lt;br&gt;
&amp;gt;  Rank(k,:) = rankt;&lt;br&gt;
&amp;gt;end&lt;br&gt;
&lt;br&gt;
&amp;gt;The Rank contains what I need. However, this is going to&lt;br&gt;
&amp;gt;take a long time, especially when the number of rows get&lt;br&gt;
&amp;gt;even larger. I found the function sort can sort the whole&lt;br&gt;
&amp;gt;matrix along one direction, however, is there any vectorized&lt;br&gt;
&amp;gt;way to get the Rank very fast without doing any looping? &lt;br&gt;
&lt;br&gt;
You can do it as a two-step process:&lt;br&gt;
&lt;br&gt;
[ind,ind] = sort(A,2);&lt;br&gt;
[Rank,Rank] = sort(ind,2);&lt;br&gt;
&lt;br&gt;
(The repeating of the variables on the left hand side is a signal&lt;br&gt;
to Matlab that the value of the first return parameter is to be&lt;br&gt;
discarded.)&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;And that's the way it is.&quot;                 -- Walter Cronkite</description>
    </item>
    <item>
      <pubDate>Thu, 05 Jun 2008 11:52:06 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#435984</link>
      <author>rych</author>
      <description>On Jun 4, 9:02 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [ind,ind] = sort(A,2);&lt;br&gt;
&amp;gt; [Rank,Rank] = sort(ind,2);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; (The repeating of the variables on the left hand side is a signal&lt;br&gt;
&amp;gt; to Matlab that the value of the first return parameter is to be&lt;br&gt;
&amp;gt; discarded.)&lt;br&gt;
&lt;br&gt;
Where is this feature documented? I mean, the discarding of the first&lt;br&gt;
output parameter?</description>
    </item>
    <item>
      <pubDate>Thu, 05 Jun 2008 16:23:50 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#436047</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;8ffc93c0-2ef6-4875-8d0d-e61b723c9e56@i76g2000hsf.googlegroups.com&amp;gt;,&lt;br&gt;
rych  &amp;lt;rychphd@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;On Jun 4, 9:02 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
&amp;gt;wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; [Rank,Rank] = sort(ind,2);&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; (The repeating of the variables on the left hand side is a signal&lt;br&gt;
&amp;gt;&amp;gt; to Matlab that the value of the first return parameter is to be&lt;br&gt;
&amp;gt;&amp;gt; discarded.)&lt;br&gt;
&lt;br&gt;
&amp;gt;Where is this feature documented? I mean, the discarding of the first&lt;br&gt;
&amp;gt;output parameter?&lt;br&gt;
&lt;br&gt;
Good question. I poked around a bit but did not find an explicit&lt;br&gt;
statement in the documentation.&lt;br&gt;
&lt;br&gt;
I could imagine the statement instead being interpreted as&lt;br&gt;
assigning the value according to the -first- output argument&lt;br&gt;
with that name and discarding the remaining ones, but I know from&lt;br&gt;
past experience that when a name is repeated, it is the -last-&lt;br&gt;
output value that has effect.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;History is a pile of debris&quot;                     -- Laurie Anderson</description>
    </item>
    <item>
      <pubDate>Thu, 05 Jun 2008 19:33:01 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#436076</link>
      <author>Roger Stafford</author>
      <description>&quot;James Anderson&quot; &amp;lt;janderson_net@yahoo.com&amp;gt; wrote in message &amp;lt;g26qnb&lt;br&gt;
$4nt$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a matrix (say 10000 rows and 50 columns). I want to&lt;br&gt;
&amp;gt; find the rank for each entry within the row it belongs to. I&lt;br&gt;
&amp;gt; know how to do this by using a loop, suppose A is the matrix&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for k = 1:10000;&lt;br&gt;
&amp;gt;   [sortx,ind] = sort(A(k,:));&lt;br&gt;
&amp;gt;   rankt(ind) = 1:50;&lt;br&gt;
&amp;gt;   Rank(k,:) = rankt;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The Rank contains what I need. However, this is going to&lt;br&gt;
&amp;gt; take a long time, especially when the number of rows get&lt;br&gt;
&amp;gt; even larger. I found the function sort can sort the whole&lt;br&gt;
&amp;gt; matrix along one direction, however, is there any vectorized&lt;br&gt;
&amp;gt; way to get the Rank very fast without doing any looping? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James&lt;br&gt;
--------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Here is a way that avoids a second sort, though I don't know if it's any faster &lt;br&gt;
than Walter's.  It depends on how large n is.  The sort would be of order &lt;br&gt;
m*n*log(n) for large n, as against order m*n for the last two lines below.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[m,n] = size(A);&lt;br&gt;
&amp;nbsp;[ig,ind] = sort(A,2);&lt;br&gt;
&amp;nbsp;[r,Rank] = ndgrid(1:m,1:n);&lt;br&gt;
&amp;nbsp;Rank(r+m*(ind-1)) = Rank;&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Thu, 05 Jun 2008 21:18:01 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#436094</link>
      <author>Jos </author>
      <description>&quot;Roger Stafford&quot; &lt;br&gt;
&amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in &lt;br&gt;
message &amp;lt;g29f1d$ra2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;James Anderson&quot; &amp;lt;janderson_net@yahoo.com&amp;gt; wrote in &lt;br&gt;
message &amp;lt;g26qnb&lt;br&gt;
&amp;gt; $4nt$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi, &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have a matrix (say 10000 rows and 50 columns). I want &lt;br&gt;
to&lt;br&gt;
&amp;gt; &amp;gt; find the rank for each entry within the row it belongs &lt;br&gt;
to. I&lt;br&gt;
&amp;gt; &amp;gt; know how to do this by using a loop, suppose A is the &lt;br&gt;
matrix&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for k = 1:10000;&lt;br&gt;
&amp;gt; &amp;gt;   [sortx,ind] = sort(A(k,:));&lt;br&gt;
&amp;gt; &amp;gt;   rankt(ind) = 1:50;&lt;br&gt;
&amp;gt; &amp;gt;   Rank(k,:) = rankt;&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The Rank contains what I need. However, this is going to&lt;br&gt;
&amp;gt; &amp;gt; take a long time, especially when the number of rows get&lt;br&gt;
&amp;gt; &amp;gt; even larger. I found the function sort can sort the &lt;br&gt;
whole&lt;br&gt;
&amp;gt; &amp;gt; matrix along one direction, however, is there any &lt;br&gt;
vectorized&lt;br&gt;
&amp;gt; &amp;gt; way to get the Rank very fast without doing any &lt;br&gt;
looping? &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; James&lt;br&gt;
&amp;gt; --------------&lt;br&gt;
&amp;gt;   Here is a way that avoids a second sort, though I don't &lt;br&gt;
know if it's any faster &lt;br&gt;
&amp;gt; than Walter's.  It depends on how large n is.  The sort &lt;br&gt;
would be of order &lt;br&gt;
&amp;gt; m*n*log(n) for large n, as against order m*n for the last &lt;br&gt;
two lines below.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  [m,n] = size(A);&lt;br&gt;
&amp;gt;  [ig,ind] = sort(A,2);&lt;br&gt;
&amp;gt;  [r,Rank] = ndgrid(1:m,1:n);&lt;br&gt;
&amp;gt;  Rank(r+m*(ind-1)) = Rank;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
and a solution that avoids ndgrid as well as a seconc call &lt;br&gt;
to sort:&lt;br&gt;
&lt;br&gt;
[m,n] = size(A);&lt;br&gt;
[ig,ind] = sort(A,2);&lt;br&gt;
Rank3 = ind + repmat(n*(0:m-1),n,1).' ;&lt;br&gt;
Rank3(Rank3) = repmat(1:n,m,1) ;&lt;br&gt;
Rank3 = reshape(Rank3,[],m).' ;&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Thu, 05 Jun 2008 22:09:02 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#436098</link>
      <author>Yi Cao</author>
      <description>&quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; wrote in message &amp;lt;g29l69&lt;br&gt;
$90t$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Roger Stafford&quot; &lt;br&gt;
&amp;gt; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in &lt;br&gt;
&amp;gt; message &amp;lt;g29f1d$ra2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &quot;James Anderson&quot; &amp;lt;janderson_net@yahoo.com&amp;gt; wrote in &lt;br&gt;
&amp;gt; message &amp;lt;g26qnb&lt;br&gt;
&amp;gt; &amp;gt; $4nt$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi, &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I have a matrix (say 10000 rows and 50 columns). I &lt;br&gt;
want &lt;br&gt;
&amp;gt; to&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; find the rank for each entry within the row it &lt;br&gt;
belongs &lt;br&gt;
&amp;gt; to. I&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; know how to do this by using a loop, suppose A is the &lt;br&gt;
&amp;gt; matrix&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; for k = 1:10000;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;   [sortx,ind] = sort(A(k,:));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;   rankt(ind) = 1:50;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;   Rank(k,:) = rankt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; The Rank contains what I need. However, this is going &lt;br&gt;
to&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; take a long time, especially when the number of rows &lt;br&gt;
get&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; even larger. I found the function sort can sort the &lt;br&gt;
&amp;gt; whole&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; matrix along one direction, however, is there any &lt;br&gt;
&amp;gt; vectorized&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; way to get the Rank very fast without doing any &lt;br&gt;
&amp;gt; looping? &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Thanks,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; James&lt;br&gt;
&amp;gt; &amp;gt; --------------&lt;br&gt;
&amp;gt; &amp;gt;   Here is a way that avoids a second sort, though I &lt;br&gt;
don't &lt;br&gt;
&amp;gt; know if it's any faster &lt;br&gt;
&amp;gt; &amp;gt; than Walter's.  It depends on how large n is.  The sort &lt;br&gt;
&amp;gt; would be of order &lt;br&gt;
&amp;gt; &amp;gt; m*n*log(n) for large n, as against order m*n for the &lt;br&gt;
last &lt;br&gt;
&amp;gt; two lines below.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;  [m,n] = size(A);&lt;br&gt;
&amp;gt; &amp;gt;  [ig,ind] = sort(A,2);&lt;br&gt;
&amp;gt; &amp;gt;  [r,Rank] = ndgrid(1:m,1:n);&lt;br&gt;
&amp;gt; &amp;gt;  Rank(r+m*(ind-1)) = Rank;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and a solution that avoids ndgrid as well as a seconc &lt;br&gt;
call &lt;br&gt;
&amp;gt; to sort:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [m,n] = size(A);&lt;br&gt;
&amp;gt; [ig,ind] = sort(A,2);&lt;br&gt;
&amp;gt; Rank3 = ind + repmat(n*(0:m-1),n,1).' ;&lt;br&gt;
&amp;gt; Rank3(Rank3) = repmat(1:n,m,1) ;&lt;br&gt;
&amp;gt; Rank3 = reshape(Rank3,[],m).' ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; hth&lt;br&gt;
&amp;gt; Jos&lt;br&gt;
&lt;br&gt;
reshape can also be avoid:&lt;br&gt;
&lt;br&gt;
[m,n]=size(A);&lt;br&gt;
[Rank,ind]=sort(A,2);&lt;br&gt;
Rank(bsxfun(@plus,(1:m)',(ind-1)*m))=repmat(1:n,m,1);&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Yi</description>
    </item>
    <item>
      <pubDate>Fri, 06 Jun 2008 08:58:02 -0400</pubDate>
      <title>Re: how to make the following case vectorized?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170467#436148</link>
      <author>Sebastiaan </author>
      <description>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in&lt;br&gt;
message &amp;lt;g293um$gs8$1@canopus.cc.umanitoba.ca&amp;gt;...&lt;br&gt;
&amp;gt; In article&lt;br&gt;
&amp;lt;8ffc93c0-2ef6-4875-8d0d-e61b723c9e56@i76g2000hsf.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt; rych  &amp;lt;rychphd@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;On Jun 4, 9:02 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter&lt;br&gt;
Roberson)&lt;br&gt;
&amp;gt; &amp;gt;wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; [Rank,Rank] = sort(ind,2);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; (The repeating of the variables on the left hand side&lt;br&gt;
is a signal&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; to Matlab that the value of the first return parameter&lt;br&gt;
is to be&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; discarded.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;Where is this feature documented? I mean, the discarding&lt;br&gt;
of the first&lt;br&gt;
&amp;gt; &amp;gt;output parameter?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Good question. I poked around a bit but did not find an&lt;br&gt;
explicit&lt;br&gt;
&amp;gt; statement in the documentation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I could imagine the statement instead being interpreted as&lt;br&gt;
&amp;gt; assigning the value according to the -first- output argument&lt;br&gt;
&amp;gt; with that name and discarding the remaining ones, but I&lt;br&gt;
know from&lt;br&gt;
&amp;gt; past experience that when a name is repeated, it is the -last-&lt;br&gt;
&amp;gt; output value that has effect.&lt;br&gt;
&lt;br&gt;
Be carefull with things like this. It works allright:&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const&lt;br&gt;
mxArray* prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double *A, *B;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (nlhs!=2)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Two output arguments are required: [a,&lt;br&gt;
b] = tester;&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B = mxGetPr(plhs[1]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B[0] = 2; /* first initialize second argument, then */&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A[0] = 1; /* overwrite the first */&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; [a, a] = tester&lt;br&gt;
a =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Going OT here, but do not expect things always to work as&lt;br&gt;
expected. Take this code:&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double *A;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (nrhs!=1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;One input/output argument is required:&lt;br&gt;
tester2(A);&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A = mxGetPr(prhs[0]);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A[0] += 1; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
i.e. overwrites the input argument (useful when working with&lt;br&gt;
large arrays - saves the need of reallocating memory when&lt;br&gt;
updating only a small part of a matrix).&lt;br&gt;
&lt;br&gt;
At a first glance, it seems to work fine, altough the&lt;br&gt;
variable is not updated in the 'workspace' tab:&lt;br&gt;
&amp;gt;&amp;gt; a = 1;&lt;br&gt;
&amp;gt;&amp;gt; testout2(a)&lt;br&gt;
&amp;gt;&amp;gt; a&lt;br&gt;
a =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;br&gt;
&lt;br&gt;
Now try this:&lt;br&gt;
&amp;gt;&amp;gt; b = a;&lt;br&gt;
&amp;gt;&amp;gt; testout2(a)&lt;br&gt;
&amp;gt;&amp;gt; a&lt;br&gt;
a =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&lt;br&gt;
&amp;gt;&amp;gt; b&lt;br&gt;
b =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&lt;br&gt;
&lt;br&gt;
Which is wrong, since b was initialized when a==2. To the&lt;br&gt;
end-user, it is unknown how Matlab handles its pointers&lt;br&gt;
internally, and might change in between Matlab versions. &lt;br&gt;
&lt;br&gt;
Ergo: only use these kind of constructions when necessary,&lt;br&gt;
and only in a tested part of the code.&lt;br&gt;
&lt;br&gt;
Greetings,&lt;br&gt;
Sebastiaan</description>
    </item>
  </channel>
</rss>

