【问题标题】:How to plot a histogram from array如何从数组中绘制直方图
【发布时间】:2012-09-10 12:22:34
【问题描述】:

我有一个大小为a=<100x1 int32> 的数组,例如a(1)=2a(2)=3 等等。如何从这些数据中绘制直方图。 当我直接尝试使用hist(a) 进行绘图时,它显示以下错误

Error using  .* 
Integers can only be combined with integers of the same class, or scalar doubles.

如果数据不是整数,假设a=<100x1 string> 这样a(1)='Saturday'a(2)='Monday' 等等,那么我该如何为这些数据绘制直方图。

【问题讨论】:

标签: matlab


【解决方案1】:

在调用 hist 之前,您必须将数据转换为双精度(或单精度,如果您担心内存):

hist(double(a));

如果你想生成一个直方图,例如字符串,您可以使用grp2idx 将数据转换为数字索引。

data = {'a' 'b' 'a' 'c'};
%# convert to numeric
[index,keys]=grp2idx(data)
index =
     1
     2
     1
     3
keys = 
    'a'
    'b'
    'c'
%# plot histogram
hist(index)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2023-03-08
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2015-08-26
    相关资源
    最近更新 更多