簡體   English   中英

如何從數組中繪制直方圖

[英]How to plot a histogram from array

我有一個大小為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' ,依此類推,那么我如何為該數據繪制直方圖。

在調用hist之前,必須將數據轉換為雙倍(如果擔心內存,則為單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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM