简体   繁体   English

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

[英]How to plot histogram of columns of a matrix in MATLAB?

I have to plot a histogram of each column of MatrixE1 .我必须绘制MatrixE1每一列的MatrixE1 How can I go about doing this?我该怎么做呢? This is what I have written so far.这是我到目前为止所写的。

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

You can access each of your coloumns in matrixE1 like this:您可以像这样访问 matrixE1 中的每个列:

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

...and then you can simply use comand hist() to plot histograms. ...然后你可以简单地使用命令 hist() 来绘制直方图。 You would plot histogram of first coloumn in matrixE1 as:您可以将 matrixE1 中第一列的直方图绘制为:

hist(firstCol);

And if I understand your second question: ''What would I do?如果我理解你的第二个问题: “我会怎么做? hist(??).历史(??)。 How can I get one histogram of all the columns of matrixE1?如何获得 matrixE1 的所有列的一个直方图? Should I do hist(matrixE1)?'' You can simply use command hold on after ploting histogram of one coloumn.我应该做 hist(matrixE1) 吗?''您可以在绘制一个柱状图的直方图后简单地使用命令保持。 Then plot another histogram on the same plot.然后在同一图上绘制另一个直方图。 For example if you want to plot histogram of first and second coloumn from matrixE1 to the same plot, you would type:例如,如果要将矩阵 E1 的第一列和第二列的直方图绘制到同一个图,您可以输入:

hist(firstCol);
hold on;
hist(secondCol);
>> 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');

在此处输入图片说明

There is another, simpler but computationally more expensive way:还有另一种更简单但计算成本更高的方法:

plotmatrix(A)

For any matrix A this will produce a m-by-n plot of scatterplots of all pairwise combinations of your input matrix ( do not do this for large matrices, larger than you could fit on your screen ).对于任何矩阵 A,这将生成输入矩阵的所有成对组合的 m×n 散点图(不要对大矩阵执行此操作,大于屏幕上的大小)。


What you gain on top are histograms along the main diagonal of the plotmatrix.您在顶部获得的是沿情节矩阵主对角线的直方图。


Adding this answer due to other answers ( 1 , 2 ) using outdated function hist .由于使用过时函数hist其他答案 ( 1 , 2 ) 添加此答案。

MATLAB recommends avoiding the use of hist and now favors histogram ( source ). MATLAB 建议避免使用hist ,现在支持histogram ( source )。 The changeover is straightforward.转换很简单。

直方图显示矩阵 A 的 3 个不同列的频率。

% 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')

You can adjust from frequency (counts) to probability or probability density function depending on the application needs with the Normalization property (see documentation here ).您可以根据应用程序需要使用Normalization属性从频率(计数)调整到概率或概率密度函数(请参阅此处的文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM