# How to draw an histogram?

14 ビュー (過去 30 日間)
Prince 2024 年 4 月 3 日
コメント済み: Prince 2024 年 4 月 4 日
How do I draw an histogram knowing the number of times all elements in a 150x150 matrix appears using matlab?

サインインしてコメントする。

### 採用された回答

Adam Danz 2024 年 4 月 3 日

> How do I draw an histogram knowing the number of times all elements in a 150x150 matrix appears using matlab?
Both histogram and histogram2 offer two types of syntaxes. One syntax lets you input the raw data so that the function can compute the density of each bin. The second lets you enter the bins and densities yourself.
It sounds like you want the second but it's unclear whether your density data are a vector (use histogram) or a matrix (use histogram2).
Here is an example of both.
histogram
counts = [12.5 38.9 4.2 9.8 28.22 11.1];
binEdges = [2 4 6 8 10 12 14];
figure()
histogram('BinEdges', binEdges, 'BinCounts', counts)
cats = categorical([2 4 6 8 10 12]);
figure()
histogram('Categories', cats, 'BinCounts', counts)
histogram2
counts = magic(5)
counts = 5x5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xBinEdges = [-2 -1 0 1 2 3];
yBinEdges = [10 20 30 40 50 60];
figure()
histogram2('XBinEdges',xBinEdges,'YBinEdges',yBinEdges,'BinCounts',counts)
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Prince 2024 年 4 月 4 日
This is the right question: How do I draw an histogram knowing the frequency of all the individual elements in a 150x150 matrix appears using matlab?

サインインしてコメントする。

### その他の回答 (1 件)

Athanasios Paraskevopoulos 2024 年 4 月 3 日
f you already have the matrix, use it directly. If not, here is an example of how you can generate a random matrix with integers for demonstration purposes:
% Generate a 150x150 matrix with random integers from 1 to 100
matrix = randi(100, 150, 150);
% Flatten the matrix to a vector
flattenedMatrix = matrix(:);
% Plot histogram using the appropriate method
histogram(flattenedMatrix, 'BinMethod', 'integers');
xlabel('Element value');
ylabel('Frequency');
title('Element frequency in 150x150 matrix');
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Prince 2024 年 4 月 4 日
This is the right question: How do I draw an histogram knowing the frequency of all the individual elements in a 150x150 matrix appears using matlab?

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by