Matlab中函数模式的问题

发布于 2025-01-01 22:49:36 字数 627 浏览 5 评论 0原文

我在 Matlab 中有一个名为 example 的双数组,如下所示:

 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   

问题是 mode(sample) 给我的结果为零,这显然是不正确的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你好,陌生人 2025-01-08 22:49:36

(这并不是真正的答案,但超出了我在评论中所能容纳的范围。)

我对此很了解,很可能是问题,或者您以某种方式重载了 mode 命令。尝试哪种模式清除模式或只是重新启动Matlab。


我无法重现,如下所示:

>> 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

如果我添加小的随机数,我可以更改答案......但不能将其设置为零。

>> 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

看起来您介于两者之间,因为您的 length(unique(sample)) 返回了 12。作为参考,我得到

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


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

(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
枕头说它不想醒 2025-01-08 22:49:36

正如 MATLAB 文档明确指出的那样,“众数函数对于离散或粗略舍入的数据最有用。连​​续概率分布的众数被定义为其密度函数的峰值。将众数函数应用于该分布的样本不太可能提供对峰值的良好估计;最好计算直方图或密度估计并计算该估计的峰值。”

这并不能解释为什么您得到 0 结果,但它应该警告您不要依赖 mode 命令来获取浮点数据。我想知道如果将 sample 变量中的值转换为整数并应用 mode 命令,您会得到什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文