如何绘制矩阵列的直方图?

时间:2016-10-14 03:33:05

标签: matlab matrix histogram

我必须绘制MatrixE1每列的直方图。我该怎么做呢?这就是我到目前为止所写的内容。非常感谢任何帮助。

% Create a random 5 x 3 matrix filled with random values between 0 and 10
a0 = 0;
b0 = 10;
r = a0 + (b0-a0).*rand(1,1);
matrixA = [randi([0 10]) randi([0 10]) randi([0 10]); randi([0 10]) randi([0 10]) randi([0 10]); randi([0 10]) randi([0 10]) randi([0 10]); randi([0 10])      randi([0 10]) randi([0 10]); randi([0 10]) randi([0 10]) randi([0 10])]
% Create identity matrix 3 x 3 
matrixB = eye(3,3) 

% Create new submatrix of A with the last 3 rows
matrixC =  matrixA(end-2 : end, :) 

%  Pair wise multiplication of C and B
matrixD = times(matrixC, matrixB)  

%  Concatenate Matrix A and D
matrixE1 = [matrixA ; matrixD]

% Plot histogram of columns. 
matrixColumn1 = matrixE1(1 : end , end-2: end-2);  
matrixFColumn2 = matrixE1(1 : end, end -1 : end-1);
matrixFColumn3 = matrixE1(1 : end, end : end); 

4 个答案:

答案 0 :(得分:2)

你可以像这样访问matrixE1中的每个coloumns:

firstCol = matrixE1(:,1);
secondCol = matrixE1(:,2);
thirdCol = matrixE1(:,3);

...然后你可以简单地使用comand hist()来绘制直方图。您可以在matrixE1中将第一个coloumn的直方图绘制为:

hist(firstCol);

如果我理解你的第二个问题: '''我该怎么办? HIST(??)。如何获得matrixE1所有列的直方图?我应该做hist(matrixE1)吗?'' 您可以在绘制一个coloumn的直方图后使用命令hold on。然后在同一图上绘制另一个直方图。例如,如果要绘制从matrixE1到同一图表的第一列和第二列的直方图,则应键入:

hist(firstCol);
hold on;
hist(secondCol);

答案 1 :(得分:1)

>> v1=randn(1000,1); % zero mean, unity stdev
>> v2=randn(1000,1)+1; % mean at one, unity stdev
>> V=[v1 v2]; % 1000 x 2 matrix
>> hist(V,100); % 100 bins
>> legend('v1', 'v2');

courier and menlo monospaced iOS fonts

答案 2 :(得分:1)

还有另一种更简单但在计算上更昂贵的方式:

plotmatrix(A)

对于任何矩阵A,这都会生成输入矩阵的所有成对组合的散点图的m-n图(对于大矩阵,不要大到屏幕上不能容纳的大小,请这样做 >)。


在顶部获得的是沿着绘图矩阵主对角线的直方图。


答案 3 :(得分:1)

使用过时的功能hist,由于其他答案(12)而添加了此答案。

MATLAB建议避免使用hist,现在建议使用histogramsource)。转换非常简单。

Histogram showing frequency of 3 difference columns of matrix A.

% MATLAB R2019a
% Sample Data
NumPoints = 2000;
a1 = 10*rand(NumPoints,1);
a2 = wblrnd(3,7,NumPoints,1);
a3 = 7 + 0.75*randn(NumPoints,1);
A = [a1 a2 a3];                      % Data Matrix

% Implement Sturges Rule for n<=500, Scott's Rule for n>500
nbinsh =@(n) ceil((1 + 3.3*log10(n))*(n<=500) + ((5/3)*(n^(1/3)))*(n>500));
NumBins = nbinsh(NumPoints);

numCols = size(A,2);

% Plot
figure, hold on
for k = 1:numCols
    histogram(A(:,k),'NumBins',NumBins,'DisplayName',['Col ' num2str(k)]);
end
legend('show')

您可以使用Normalization属性(请参见documentation here)根据应用程序的需要,从频率(计数)到概率或概率密度函数进行调整。

相关问题