Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to make the following case vectorized?
Date: Fri, 6 Jun 2008 08:58:02 +0000 (UTC)
Organization: Erasmusmc
Lines: 104
Message-ID: <g2au6q$hgd$1@fred.mathworks.com>
References: <g26qnb$4nt$1@fred.mathworks.com> <g26sc0$2vn$1@canopus.cc.umanitoba.ca> <8ffc93c0-2ef6-4875-8d0d-e61b723c9e56@i76g2000hsf.googlegroups.com> <g293um$gs8$1@canopus.cc.umanitoba.ca>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1212742682 17933 172.30.248.35 (6 Jun 2008 08:58:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 6 Jun 2008 08:58:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1095751
Xref: news.mathworks.com comp.soft-sys.matlab:472494



roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g293um$gs8$1@canopus.cc.umanitoba.ca>...
> In article
<8ffc93c0-2ef6-4875-8d0d-e61b723c9e56@i76g2000hsf.googlegroups.com>,
> rych  <rychphd@gmail.com> wrote:
> >On Jun 4, 9:02 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter
Roberson)
> >wrote:
> 
> >> [Rank,Rank] = sort(ind,2);
> 
> >> (The repeating of the variables on the left hand side
is a signal
> >> to Matlab that the value of the first return parameter
is to be
> >> discarded.)
> 
> >Where is this feature documented? I mean, the discarding
of the first
> >output parameter?
> 
> Good question. I poked around a bit but did not find an
explicit
> statement in the documentation.
> 
> I could imagine the statement instead being interpreted as
> assigning the value according to the -first- output argument
> with that name and discarding the remaining ones, but I
know from
> past experience that when a name is repeated, it is the -last-
> output value that has effect.

Be carefull with things like this. It works allright:

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const
mxArray* prhs[])
{
    double *A, *B;
    if (nlhs!=2)
        mexErrMsgTxt("Two output arguments are required: [a,
b] = tester;");

    plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
    plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);

    A = mxGetPr(plhs[0]);
    B = mxGetPr(plhs[1]);
    
    B[0] = 2; /* first initialize second argument, then */
    A[0] = 1; /* overwrite the first */
    return;
}

>> [a, a] = tester
a =
     2


Going OT here, but do not expect things always to work as
expected. Take this code:
{
    double *A;
    if (nrhs!=1)
        mexErrMsgTxt("One input/output argument is required:
tester2(A);");

    A = mxGetPr(prhs[0]);

    A[0] += 1; 
    
    return;
}

i.e. overwrites the input argument (useful when working with
large arrays - saves the need of reallocating memory when
updating only a small part of a matrix).

At a first glance, it seems to work fine, altough the
variable is not updated in the 'workspace' tab:
>> a = 1;
>> testout2(a)
>> a
a =
     2

Now try this:
>> b = a;
>> testout2(a)
>> a
a =
     3
>> b
b =
     3

Which is wrong, since b was initialized when a==2. To the
end-user, it is unknown how Matlab handles its pointers
internally, and might change in between Matlab versions. 

Ergo: only use these kind of constructions when necessary,
and only in a tested part of the code.

Greetings,
Sebastiaan