我按升序排列代码。我需要正常分发数据。我需要随机和随机24.我怀疑中小型数组可以做正态分布,也可以找到均值和标准差。因此,我如何做随机8和随机24 以下是我的代码:
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 ')
我收到错误消息 未定义的函数'sum'表示'cell'类型的输入参数。
Error in mean (line 116)
y = sum(x, dim, flag)/size(x,dim);
Error in RANDOM (line 15)
mean8= mean(B);
答案 0 :(得分: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);
然后保持最后一行检查输出:
st = [ st' B rnd8 ]