Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 分布直方图_Matlab - Fatal编程技术网

Matlab 分布直方图

Matlab 分布直方图,matlab,Matlab,嗨,我正在尝试使用堆栈溢出的一些代码制作一个简单的分布直方图 然而,我无法让它工作。我知道使用统计工具箱有一个简单的方法,但从学习的角度来看,我更喜欢解释性更强的代码——有人能帮我吗 %% clear all load('Mini Project 1.mat') % Set data to var2 data = var2; % Set the number of bins nbins = 0:.8:8; % Create a histogram plot of data sort

嗨,我正在尝试使用堆栈溢出的一些代码制作一个简单的分布直方图 然而,我无法让它工作。我知道使用统计工具箱有一个简单的方法,但从学习的角度来看,我更喜欢解释性更强的代码——有人能帮我吗

%% 

clear all

load('Mini Project 1.mat')

% Set data to var2
data = var2; 

% Set the number of bins
nbins = 0:.8:8;

% Create a histogram plot of data sorted into (nbins) equally spaced bins
n = hist(data,nbins);

% Plot a bar chart with y values at each x value. 
% Notice that the length(x) and length(y) have to be same.
bar(nbins,n);

MEAN = mean(data);
STD = sqrt(mean((data - MEAN).^2)); % can also use the simple std(data)


f = (   1/(STD*sqrt(2*pi))   )  *  exp(-0.5*((nbins-MEAN)/STD).^2  );
f = f*sum(nbins)/sum(f);

hold on; 

% Plots a 2-D line plot(x,y) with the normal distribution, 
% c = color cyan , Width of line = 2
plot (data,f, 'c', 'LineWidth', 2);

xlabel('');
ylabel('Firmness of apples after one month of storage')
title('Histogram compared to normal distribution');

hold of
你弄糊涂了

hist

仔细阅读这两篇文章


另外,您不是在定义存储箱的数量,而是在定义存储箱本身。

我现在手头没有Matlab,但请尝试以下操作:

如果要将正态分布与条形图比较
条形图(nbins,n)
,应首先将其标准化:

bar(nbins,n/sum(n))
看看这是否解决了你的问题。
如果没有,请尝试删除行
f=f*sum(nbins)/sum(f)

更新:MATLAB的
直方图
命令有一个结构化的输出,现在可以更轻松地进行此操作。
bar(nbins,n/sum(n))