2017-09-06 2 views
0

Kann jemand erklären die zwei Zeilen des Codes, die unten hervorgehoben werden, die repmat verwenden? Dies wird documentation for learning data analysis direkt von dem MathWorks genommen:Matlab: repmat Code Erklärung

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 

Könnte jemand die X = repmat(...) Linie mir das erklären? Ich weiß, dass es für die 1 und 2 Standardabweichungslinien geplottet wird.

Auch habe ich versucht, die Y = ... Zeile kommentieren, und die Handlung sieht genau das gleiche, also was ist der Zweck dieser Linie?

Dank

Antwort

2

Lets den Ausdruck in mehrere Anweisungen

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

entspricht

% 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]; 

brechen die Logik für die Y Matrix ähnlich ist, außer dass es in der Spaltenrichtung wiederholt. Gemeinsam am Ende mit

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

up Wenn Plot genannt wird eine Zeile pro Spalte der X und Y Matrizen Plots.

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

entspricht

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); 

die zwei vertikalen Linien Plots, ein und zwei Standardabweichungen vom Mittelwert. Wenn Sie Y auskommentieren, ist Y nicht definiert. Der Grund, warum der Code noch funktionierte, ist wahrscheinlich, dass der vorherige Wert Y noch im Arbeitsbereich gespeichert wurde. Wenn Sie den Befehl clear ausführen, bevor Sie das Skript erneut ausführen, werden Sie feststellen, dass der Befehl plot fehlschlägt.