<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260229</link>
    <title>MATLAB Central Newsreader - unique() Help</title>
    <description>Feed for thread: unique() Help</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>Mon, 07 Sep 2009 15:58:02 -0400</pubDate>
      <title>unique() Help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260229#678542</link>
      <author>Husam Aldahiyat</author>
      <description>Hello,&lt;br&gt;
I need a function that outputs the unique elements of each column of my desired matrix.&lt;br&gt;
I can use &lt;br&gt;
sum(~~diff(sort(A)))+1&lt;br&gt;
to get the number of unique elements in each column of a matrix A,&lt;br&gt;
but can't I produce a cell or zero padded matrix for what the unique elements are?&lt;br&gt;
I used a loop and it takes 99% of my calculation time and is killing me!&lt;br&gt;
&lt;br&gt;
Help!!!</description>
    </item>
    <item>
      <pubDate>Mon, 07 Sep 2009 16:06:41 -0400</pubDate>
      <title>Re: unique() Help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260229#678545</link>
      <author>dpb</author>
      <description>Husam Aldahiyat wrote:&lt;br&gt;
&amp;gt; I need a function that outputs the unique elements of each column of my desired matrix.&lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
How about (for matrix x)&lt;br&gt;
&lt;br&gt;
for i=1:size(x,2), u{i} = unique(x(:,i)); end&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Mon, 07 Sep 2009 16:16:40 -0400</pubDate>
      <title>Re: unique() Help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260229#678546</link>
      <author>arun</author>
      <description>On Sep 7, 5:58&#160;pm, &quot;Husam Aldahiyat&quot; &amp;lt;numand...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hello,&lt;br&gt;
&amp;gt; I need a function that outputs the unique elements of each column of my desired matrix.&lt;br&gt;
&amp;gt; I can use&lt;br&gt;
&amp;gt; sum(~~diff(sort(A)))+1&lt;br&gt;
&amp;gt; to get the number of unique elements in each column of a matrix A,&lt;br&gt;
&amp;gt; but can't I produce a cell or zero padded matrix for what the unique elements are?&lt;br&gt;
&amp;gt; I used a loop and it takes 99% of my calculation time and is killing me!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Help!!!&lt;br&gt;
&lt;br&gt;
given matrix y (with n columns),&lt;br&gt;
u = arrayfun(@(x) unique(y(:,x)), 1:size(y,2), 'uni', false);&lt;br&gt;
will give a 1*n cell array with each element holding the unique&lt;br&gt;
elements of the column.&lt;br&gt;
best, arun.</description>
    </item>
    <item>
      <pubDate>Mon, 07 Sep 2009 16:44:02 -0400</pubDate>
      <title>Re: unique() Help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260229#678549</link>
      <author>Husam Aldahiyat</author>
      <description>dpb, thanks for the effort but I already said no loops.&lt;br&gt;
&lt;br&gt;
arun, thanks alot!!!!!!!!!!!!! :)</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 03:43:05 -0400</pubDate>
      <title>Re: unique() Help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260229#678643</link>
      <author>Matt Fig</author>
      <description>With a little prep work, the arrayfun solution can be made faster:&lt;br&gt;
&lt;br&gt;
%%%&lt;br&gt;
t = sort(y);&lt;br&gt;
d = [true(1,size(y,2)); 0~=(diff(t))]; % Replace call to unique with index into t.&lt;br&gt;
U = arrayfun(@(x) t(d(:,x),x),1:size(y,2),'Un',0);  % Slow because of anon. func.&lt;br&gt;
%%%&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
But this is still NOT as fast as the For loop:&lt;br&gt;
&lt;br&gt;
%%%&lt;br&gt;
t = sort(y);&lt;br&gt;
d = [true(1,size(y,2)); 0~=(diff(t))]; &lt;br&gt;
U2 = cell(1,size(y,2));&lt;br&gt;
&lt;br&gt;
for ii = 1:size(y,2)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;U2{ii} = t(d(:,ii),ii);  % Replace call to unique with index into t.&lt;br&gt;
end&lt;br&gt;
%%%&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
For a random array 30-by-2000, the For loop is 4 times faster than the above arrayfun, and 15 times faster than the one line arrayfun approach, at least using 2007b.</description>
    </item>
  </channel>
</rss>

