2017-02-15 57 views
1

我安排我的升序排列的代碼。我需要做一個正常的數據分佈。我需要有random8和隨機24.我懷疑大中小型的陣列可以做正態分佈,也發現了平均值和標準偏差。因此,我怎麼能隨機8做,random24 下面是我的代碼:如何計算的立場偏差和平均

symbols = {'SMALL', 'MEDIUM', 'LARGE'}; 
    range = 101; % <-- Or 101 if you want from 0 to 100 with 100 included 
    rndInd = randi(numel(symbols), range ,1); 
    st = symbols(rndInd); 
    strjoin(st); 
    random24 = strtrim(cellstr(num2str(randi(24, range, 1)))); 
    random8 = strtrim(cellstr(num2str(randi(8, range, 1)))); 
    b = num2cell(sort(randi(24, range, 1))); 
    B = cellfun(@num2str, b, 'UniformOutput', false); 
    %B = sort(random24) 
    st = [ st' B random8 ] 

    %% MEAN 

    mean8=mean(random8); 
    fprintf('Mean of Random 8 parking Duration is %f',mean8); 

     mean24=mean(random24); 
     fprintf('Mean of Random 24 Hours is %f',mean24); 

     %% Standard deviation 
     standDev8= std(random8); 
     fprintf('Standard Deviation of Random 1-8 Hours of parking Duration    is %f',standDev8); 
     figure(1); 
     histogram(random8); 
     title('Histogram of Data for Random 1-8 Hours of parking Duration') 

     standDev24= std(random24); 
     fprintf('Standard Deviation of Random 24 Hours is %f',standDev24); 
     figure(2); 
     histogram(random24); 
     title('Histogram of Data for Random 24Hours ') 

錯誤消息我得到 未定義功能「和」類型「細胞」的輸入參數。

 Error in mean (line 116) 
     y = sum(x, dim, flag)/size(x,dim); 

     Error in RANDOM (line 15) 
     mean8= mean(B); 

回答

1

問題是您正在排序字符串而不是數字。這就是爲什麼你得到你看到的結果。一個簡單的解決辦法是數字的排列第一排序,然後將其轉換成字符串列表。

所以,你的腳本的第一部分可以是不變的:

symbols = {'SMALL', 'MEDIUM', 'LARGE'}; 
N = 101; % <-- Or 101 if you want from 0 to 100 with 100 included 
rndInd = randi(numel(symbols), N ,1); 
st = symbols(rndInd); 
strjoin(st); 

您可以將下面一行不變,以及如果你不需要用它做任何事情:

rnd8 = strtrim(cellstr(num2str(randi(8, N, 1)))); 

然後你可以做這樣的事情(也許有更好的選擇):

b = num2cell(sort(randi(24, N, 1))); 
B = cellfun(@num2str, b, 'UniformOutput', false); 

And t母雞保持你的最後一行檢查輸出:

st = [ st' B rnd8 ]