Search code examples
arraysmatlabmode

Finding the mode of floating point arrays in MatLab


I have an array of about 25,000 4-D vectors that vary around
[1 .001 .0015 .0000010434].

I'm trying to find the mode of the vectors. I've tried MatLab's built-in mode() function, but I get a result of 1 0 0 0. Could it be that because the decimals are so small that MatLab's mode() function rounds to a certain decimal point, resulting in 0? Is there a way to bypass this? I've also read about histc()'s functionality, but don't understand how I can reach my goal using it.

Any help is appreciated, thanks in advance.


Solution

  • You can use unique to find repetition number of each row in its result:

     % A is a  25000 * 4 matrix
     [C,~,ic] = unique(A,'rows'); % find unique rows
    

    in the above, ic shows the index of the rows. Hence, the mode of these can determine the most repetitive vector:

     modeRow = A(mode(ic),:);