2017-09-06 90 views
0

任何人都可以解释下面突出显示的两行代码,它使用repmatMatlab:repmat代码解释

bin_counts = hist(c3); % Histogram bin counts 
N = max(bin_counts); % Maximum bin count 
mu3 = mean(c3);   % Data mean 
sigma3 = std(c3);  % Data standard deviation 

hist(c3) % Plot histogram 
hold on 
plot([mu3 mu3],[0 N],'r','LineWidth',2) % Mean 
% -------------------------------------------------------------- 
X = repmat(mu3+(1:2)*sigma3,2,1);  % WHAT IS THIS? 
Y = repmat([0;N],1,2);     % WHY IS THIS NECESSARY? 
% -------------------------------------------------------------- 
plot(X,Y,'g','LineWidth',2) % Standard deviations 
legend('Data','Mean','Stds') 
hold off 

谁能解释X = repmat(...)行对我说:这是直接从MathWorks公司documentation for learning data analysis采取?我知道它将被绘制为1和2的标准偏差线。

此外,我试着评论出Y = ...这一行,而且情节看起来完全一样,那么这一行的目的是什么?

由于

回答

2

允许打破表达成多个语句

X = repmat(mu3+(1:2)*sigma3,2,1); 

相当于

% First create a row vector containing one and two standard deviations from the mean. 
% This is equivalent to xvals = [mu3+1*sigma3, mu3+2*sigma3]; 
xval = mu3 + (1:2)*sigma3; 

% Repeat the matrix twice in the vertical dimension. We want to plot two vertical 
% lines so the first and second point should be equal so we just use repmat to repeat them. 
% This is equivalent to 
% X = [xvals; 
%  xvals]; 
X = repmat(xval,2,1); 

% To help understand how repmat works, if we had X = repmat(xval,3,2) we would get 
% X = [xval, xval; 
%  xval, xval; 
%  xval, xval]; 

的逻辑是用于Y矩阵类似,除了它在列方向上重复。你一起结束了

X = [mu3+1*sigma3, mu3+2*sigma3; 
    mu3+1*sigma3, mu3+2*sigma3]; 
Y = [0, 0; 
    N, N]; 

当阴谋被称之为绘制每XY矩阵的列一行。

plot(X,Y,'g','LineWidth',2); 

相当于

plot([mu3+1*sigma3; mu3+1*sigma3], [0, N], 'g','LineWidth',2); 
hold on; 
plot([mu3+2*sigma3; mu3+2*sigma3], [0, N], 'g','LineWidth',2); 

图表两条垂直线,和一个与平均值的两个标准差。

如果您评论为YY未定义。代码仍然有效的原因可能是前一个值Y仍然存储在工作区中。如果在再次运行脚本之前运行命令clear,则会发现plot命令将失败。