Academia
Academia

Reputation: 4124

Problems with function mode in Matlab

I have a double array named sample in Matlab as shown below:

 sample = ...
  [0.4375 0.3750 0.5313 0.4375 0.8750 0.3750 0.5313 0.6563 0.3750 0.4375 ...
   0.5313 0.5313 0.8438 0.8438 0.4375 0.5313 0.5313 0.5313 0.4375 0.3750 ...
   0.4375 0.3750 0.3750 0.5313 0.3750 0.8750 0.5313 0.8438 0.4375 0.0313 ...
   0.3750 0.8438 0.8438 0.3750 0.8750 0.8750 0.5313 0.4375 0.8750 1.7813 ...
   0.3750 0.8750 0.3750 0.3750 0.3750 0.4375 0.3750 0.3750 0.8750 0.3750];


whos sample
  Name        Size            Bytes  Class     Attributes
  sample      1x50              400  double   

The problem is that mode(sample) gives me zero as a result and this obviously is not correct.

Upvotes: 0

Views: 824

Answers (2)

anon
anon

Reputation:

As MATLAB documentation clearly states "the mode function is most useful with discrete or coarsely rounded data. The mode for a continuous probability distribution is defined as the peak of its density function. Applying the mode function to a sample from that distribution is unlikely to provide a good estimate of the peak; it would be better to compute a histogram or density estimate and calculate the peak of that estimate."

This doesn't explain why you are getting 0 as a result, but it should caution you not to rely on the mode command for floating point data. I wonder what you would get if you turned the values in the sample variable into integers and apply the mode command.

Upvotes: 0

Pursuit
Pursuit

Reputation: 12345

(This is not really an answer, but more than I can fit in a comment.)

I'm with gnovice on this, most likely problem or that you have overloaded the mode command somehow. Try which mode, clear mode or just restarting Matlab.


I cannot reproduce, as shown below:

>> sample =[...
    0.4375    0.3750    0.5313    0.4375    0.8750    0.3750    ...
    0.5313    0.6563    0.3750    0.4375    0.5313 ...
    0.5313    0.8438    0.8438    0.4375    0.5313    0.5313    ...
    0.5313    0.4375    0.3750    0.4375    0.3750 ...
    0.3750    0.5313    0.3750    0.8750    0.5313    0.8438    ...
    0.4375    0.0313    0.3750    0.8438    0.8438 ...
    0.3750    0.8750    0.8750    0.5313    0.4375    0.8750    ...
    1.7813    0.3750    0.8750    0.3750    0.3750 ...
    0.3750    0.4375    0.3750    0.3750    0.8750    0.3750];
>> mode(sample)
ans =
                 0.375

If I add small random numbers, I can change the answer ... but not set it to zero.

>> format short g
>> sample = sample .* (1+100*eps*randn(size(sample)))
sample =
  Columns 1 through 11
       0.4375        0.375       0.5313       0.4375        0.875        0.375       0.5313       0.6563        0.375       0.4375       0.5313
  Columns 12 through 22
       0.5313       0.8438       0.8438       0.4375       0.5313       0.5313       0.5313       0.4375        0.375       0.4375        0.375
  Columns 23 through 33
        0.375       0.5313        0.375        0.875       0.5313       0.8438       0.4375       0.0313        0.375       0.8438       0.8438
  Columns 34 through 44
        0.375        0.875        0.875       0.5313       0.4375        0.875       1.7813        0.375        0.875        0.375        0.375
  Columns 45 through 50
        0.375       0.4375        0.375        0.375        0.875        0.375

>> mode(sample)
ans =
       0.0313

It looks like you are somewhere between, since your length(unique(sample)) returned 12. For reference, I get

>> length(unique(sample))  %After the initial setup above
ans =
     8


>> length(unique(sample))  %After adding small random perturbations
ans =
    50

Upvotes: 3

Related Questions