Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest way to find unique values in an array
    primarykey
    data
    text
    <p>I'm trying to find a fastest way for finding unique values in a array and to remove <code>0</code> as a possibility of unique value.</p> <p>Right now I have two solutions:</p> <pre><code>result1 = setxor(0, dataArray(1:end,1)); % This gives the correct solution result2 = unique(dataArray(1:end,1)); % This solution is faster but doesn't give the same result as result1 </code></pre> <p><code>dataArray</code> is equivalent to :</p> <pre><code>dataArray = [0 0; 0 2; 0 4; 0 6; 1 0; 1 2; 1 4; 1 6; 2 0; 2 2; 2 4; 2 6]; % This is a small array, but in my case there are usually over 10 000 lines. </code></pre> <p>So in this case, <code>result1</code> is equal to <code>[1; 2]</code> and <code>result2</code> is equal to <code>[0; 1; 2]</code>. The <code>unique</code> function is faster but I don't want <code>0</code> to be considered. Is there a way to do this with <code>unique</code> and not consider <code>0</code> as a unique value? Is there an another alternative?</p> <p><strong>EDIT</strong></p> <p>I wanted to time the various solutions.</p> <pre><code>clc dataArray = floor(10*rand(10e3,10)); dataArray(mod(dataArray(:,1),3)==0)=0; % Initial tic for ii = 1:10000 FCT1 = setxor(0, dataArray(:,1)); end toc % My solution tic for ii = 1:10000 FCT2 = unique(dataArray(dataArray(:,1)&gt;0,1)); end toc % Pursuit solution tic for ii = 1:10000 FCT3 = unique(dataArray(:, 1)); FCT3(FCT3==0) = []; end toc % Pursuit solution with chappjc comment tic for ii = 1:10000 FCT32 = unique(dataArray(:, 1)); FCT32 = FCT32(FCT32~=0); end toc % chappjc solution tic for ii = 1:10000 FCT4 = setdiff(unique(dataArray(:,1)),0); end toc % chappjc 2nd solution tic for ii = 1:10000 FCT5 = find(accumarray(dataArray(:,1)+1,1))-1; FCT5 = FCT5(FCT5&gt;0); end toc </code></pre> <p>And the results:</p> <pre><code>Elapsed time is 5.153571 seconds. % FCT1 Initial Elapsed time is 3.837637 seconds. % FCT2 My solution Elapsed time is 3.464652 seconds. % FCT3 Pursuit solution Elapsed time is 3.414338 seconds. % FCT32 Pursuit solution with chappjc comment Elapsed time is 4.097164 seconds. % FCT4 chappjc solution Elapsed time is 0.936623 seconds. % FCT5 chappjc 2nd solution </code></pre> <p>However, the solution with <code>sparse</code> and <code>accumarray</code> only works with <code>integer</code>. These solutions won't work with <code>double</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload