2012-01-29 27 views
0

我有一個大小爲a=<100x1 int32>的數組,例如a(1)=2,a(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'等那我怎麼才能繪製直方圖這個數據。

+2

難道你只是問這個問題在這裏:http://stackoverflow.com/questions/9055541/plotting-a-histogram-from-cellvalues? – 2012-01-29 18:46:14

回答

3

你有你的數據轉換爲加倍(或單,如果你擔心內存)調用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)