unique function Matlab<->Julia

786 views
Skip to first unread message

Stephan Buchert

unread,
Apr 21, 2015, 5:16:12 AM4/21/15
to julia...@googlegroups.com
In Matlab I can do, for example, with "daynum" fractional day numbers:

idaynum = floor(daynum);
[umjd, im, iu] = unique(idaynum);  % <= how do I get the "iu" in Julia?

% I need an observation "pms" listed per day in a file.
% For performance reasons the parameter is retrieved from the file only once for each of the different integer days
upms
= pi*umjd;   % <= for illustration only, instead of really retrieving the observed parameter
pms
= upms(iu);
% Now "pms" matches "daynum", ie. when daynum=[1.0, 1.5, 1.8, 2.0, 2.2, 3.1], then pms approx. [3.14, 3.14, 3.14, 6.28, 6.28, 9.42]

Probably this can be done elegantly also in Julia, but, coming from Matlab, for me not easy to see how?

Tomas Lycken

unread,
Apr 21, 2015, 11:28:57 AM4/21/15
to julia...@googlegroups.com
For completeness, here is an excerpt from the MATLAB help file on `unique`:

[C,ia,ic] = unique(A) also returns index vectors ia and ic.

  • If A is a vector, then C = A(ia) and A = C(ic).

  • If A is a matrix or array, then C = A(ia) and A(:) = C(ic).

  • If A is a table, then C = A(ia,:) and A = C(ic,:).


Which of the cases is relevant for you?

// T

Stephan Buchert

unread,
Apr 22, 2015, 1:11:21 AM4/22/15
to julia...@googlegroups.com
The case "A is a vector" is relevant for this, and I need "ic", for A=C(ic)

Steven G. Johnson

unread,
Apr 22, 2015, 8:13:00 AM4/22/15
to julia...@googlegroups.com


On Tuesday, April 21, 2015 at 5:16:12 AM UTC-4, Stephan Buchert wrote:
In Matlab I can do, for example, with "daynum" fractional day numbers:

idaynum = floor(daynum);
[umjd, im, iu] = unique(idaynum);  % <= how do I get the "iu" in Julia?

This is not implemented yet; see:

https://github.com/JuliaLang/julia/issues/1845

Stephan Buchert

unread,
Apr 22, 2015, 9:00:36 AM4/22/15
to julia...@googlegroups.com
Ok, thanks. At last here is a use case.

Tim Holy

unread,
Apr 22, 2015, 9:17:24 AM4/22/15
to julia...@googlegroups.com
Interestingly, even though I use(d) the 3-output `unique` in Matlab on an
almost daily basis, in Julia I never find I need/want it---I didn't even
realize we didn't have it. I confess to not being sure what the difference is,
but I suspect it's because there must be better approaches in julia for the
problems for which I would have used `unique`.

--Tim

Dawid Crivelli

unread,
Apr 22, 2015, 9:27:03 AM4/22/15
to julia...@googlegroups.com
You can probably get around it, by using the `indexin` function

redunant = rand(1:4,10)    # [1,3,3,4,3,1,3,3,3,4]
slimmed
= unique(redunant) # returns in my case [1,2,3,4]

indexin
(slimmed, redunant) # returns [6, 9, 10], which is one of the possibilities


It's almost easier to modify either `unique` or modify one of the `findin` or `indexin` functions to make your own unique which returns indexes
Reply all
Reply to author
Forward
0 new messages