2017-09-06 122 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命令將失敗。