Re: Occurrences in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg106366] Re: [mg106350] Occurrences in Mathematica
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Mon, 11 Jan 2010 05:26:46 -0500 (EST)
- References: <201001100828.DAA05428@smc.vnet.net>
Hi George,
Here is a test list:
In[1]:=
test = RandomInteger[{1,30},30]
Out[1]=
{13,1,27,11,16,21,4,17,13,22,26,29,28,14,27,1,6,12,21,12,13,4,19,1,21,28,14,6,20,5}
One way to do what you want:
In[2]:=
Select[Tally[test],Last@#==1&][[All,1]]
Out[2]= {11,16,17,22,26,29,19,20,5}
If your elements are integers (or reals), you can make this code quite a bit
faster by compiling it:
In[3] =
largetest = RandomInteger[{1, 30000}, 30000];
In[4]:=
fn = Compile[{{lst,_Integer,1}},
Select[Tally[lst],Last@#==1&][[All,1]],{{Tally[_],_Integer,2}}]
Out[4]=
CompiledFunction[{lst},Select[Tally[lst],Last[#1]==1&][[All,1]],-CompiledCode-]
In[5]:=
(res1 = Select[Tally[largetest],Last@#==1&][[All,1]]);//Timing
Out[5]= {0.231,Null}
In[6]:==
(res2 = fn[largetest]); // Timing
Out[6]= {0.07, Null}
In[7]:=
res1 == res2
Out[7]= True
Hope this helps.
Regards,
Leonid
On Sun, Jan 10, 2010 at 12:28 AM, George <gtatishvili at gmail.com> wrote:
> Hi,
>
> I have a very big list of elements and I need Mathematica to give me
> only those elements which occure only once...
>
> Could anybody advise me please how to write the code for that?
>
> Thks much
> George
>
>
- References:
- Occurrences in Mathematica
- From: George <gtatishvili@gmail.com>
- Occurrences in Mathematica