Reputation: 13
I have to plot a histogram of each column of 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);
Upvotes: 1
Views: 4943
Reputation: 2854
Adding this answer due to other answers (1, 2) using outdated function hist
.
MATLAB recommends avoiding the use of hist
and now favors histogram
(source). The changeover is straightforward.
% 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).
Upvotes: 1
Reputation: 2865
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).
What you gain on top are histograms along the main diagonal of the plotmatrix.
Upvotes: 1
Reputation: 296
You can access each of your coloumns in matrixE1 like this:
firstCol = matrixE1(:,1);
secondCol = matrixE1(:,2);
thirdCol = matrixE1(:,3);
...and then you can simply use comand hist() to plot histograms. You would plot histogram of first coloumn in matrixE1 as:
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? Should I do hist(matrixE1)?'' You can simply use command hold on after ploting histogram of one coloumn. 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:
hist(firstCol);
hold on;
hist(secondCol);
Upvotes: 3
Reputation: 4076
>> 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');
Upvotes: 2