From: Matt Fig on
"Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hi290i$dsp$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hi24al$3ig$1(a)fred.mathworks.com>...
> > "Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hi2300$6dk$1(a)fred.mathworks.com>...
> > > "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message
> > > * snip *
> > > > It would be nice to have a function SORTIND with replying just the sorting index. This would save time in many of my functions. Is this implemented already?
> > >
> > >
> > > Hi Jan,
> > >
> > > Do you mean the equivalent of the following function
> > >
> > > function SortIndex = sortind(varargin)
> > > [Dummy, SortIndex] = sort(varargin{:})
> > >
> > > Jos
> >
> > Of course, newer Matlab has the "titled" syntax for optional input/output arguments, and this can be applied for all functions.
> >
> > I tend not to use it because of the backward incompatible.
> >
> > Bruno
>
> I agree with you on this, Bruno. I'll wait till I have no more access to old versions before using the tilde.
>
> However, I doubt using the tilde is faster than using the dummy variable, since the memory for this variable is created within sort itself. I do not think that memory is actually being copied to the dummy variable.
>
> Jos



Though it shouldn't make any speed difference internally, is there a reason you don't avoid creating the dummy variable in the first place? I.E.,

[idx,idx] = sort(A);

instead of

[dummy,idx] = sort(A);

To me they are equally readable, but that could be only because I am used to doing this. Obviously this technique doesn't work when it is the SECOND return argument that is the dummy.

Just curious.
From: us on
"Matt Fig"
> Though it shouldn't make any speed difference internally, is there a reason you don't avoid creating the dummy variable in the first place? I.E.,
> [idx,idx] = sort(A);
> instead of
> [dummy,idx] = sort(A);
>
> To me they are equally readable, but that could be only because I am used to doing this. Obviously this technique doesn't work when it is the SECOND return argument that is the dummy.
> Just curious.

this has been shown many a times in CSSM...
- i'll always use
[foo,foo,foo,goo,goo]=f();
approach
- the newly introduced ~ - in my view - is a waste of precious syntax resources...

format debug;
[a,b]=max([1,2]) % <- creates TWO vars with diff addresses
[c,c]=max([1,2]) % <- both have the same address indep of their position
%{
a =
Structure address = afcc988
pr = 2031f3b0
b =
Structure address = afcf2a8 % <- ~= A (or, typically named DUMMY)
pr = 2031f750 % <- ~= A (...)
c =
Structure address = afcaab0
pr = 2031ee10
c =
Structure address = afcaab0 % <- == 1.st C
pr = 2031ee10 %< == 1.st C
%}

us
From: Matt Fig on
"us " <us(a)neurol.unizh.ch> wrote in message
> this has been shown many a times in CSSM...
> - i'll always use
> [foo,foo,foo,goo,goo]=f();
> approach


I know you use this approach, as I do. I was asking why other folks prefer to create a dummy variable in the workspace.


> - the newly introduced ~ - in my view - is a waste of precious syntax resources...
>


I agree, except in the case I mentioned previously ;-).
From: Bruno Luong on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hi2im0$fbd$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message
> > this has been shown many a times in CSSM...
> > - i'll always use
> > [foo,foo,foo,goo,goo]=f();
> > approach
>
>
> I know you use this approach, as I do. I was asking why other folks prefer to create a dummy variable in the workspace.

Matt and us, I for once prefer the "dummy" approach (followed by a "clear" statement). It's just more readable to me (and I force myself to use different names for different variables). Any eloquence argument to convince me otherwise?

Bruno
From: Bruno Luong on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hi25nk$5lo$1(a)fred.mathworks.com>...

>
> Exactly. Creating [Dummy] wastes time in many of my functions.

Jan, how much do you estimate an inplace sorting (e.g., on large double array) would save time?

I'm quite happy with the performance of matlab SORT, except I wish to have a sorting routine where the comparison operator can be customized - but I guess such feature is very inefficient in Matlab due to overhead.

Bruno