通过将数字分组到一个范围内在 matlab/octave 中绘图
Plotting in matlab / octave by grouping numbers into a range
我试图让 数据成为 x 轴 并且 y 轴成为按 10[=27= 分组的值的数量].请参见下面的图作为示例
我尝试使用 bar(x),但我不确定您如何获得正确类别中的数字以进行绘图。
示例:
如果数据是 x=1,5.3,9,10.5,12,13,15.2,25,191,192.4
the group 0-10 should be 1,5.3,9
the group 10.1-20 should be 10.5,12,13,15.2
the group of 20.1-30 should be 25
.
.
.
the group of 190.1-200 should be 191,192.4
PS: 我使用的是 octave 3.8.1,它类似于 matlab
您可以使用 histc
。但是 histc
认为每个 bin 的左边缘相等,而不是右边缘:
bincounts = histc(x,binranges)
counts the number of values in x
that are within each specified bin range. The input, binranges
, determines the endpoints for each bin. The output, bincounts
, contains the number of elements from x
in each bin.
For example, if binranges
equals the vector [0,5,10,13]
, then histc
creates four bins. The first bin includes values greater than or equal to 0
and strictly less than 5
. The second bin includes values greater than or equal to 5
and less than 10
, and so on. The last bin contains the scalar value 13
.
要设置左边缘的相等条件,最好用 bsxfun
:
手动设置
y = diff(sum(bsxfun(@le, x(:), 0:10:200), 1));
sum(bsxfun(...), 1)
查找 x
中有多少条目小于或等于 0
、10
、... 200
;然后 diff(...)
给出了想要的结果 y
:
y =
3 4 1 0 0 0 0 [...] 2
然后您可以使用 bar(y)
绘制条形图。如果您还想更改 x 轴上显示的文本,请设置轴的 'xticklabel'
属性:
bar(y);
strings = {'0-10', '10-20', '20-30'}; %// manually define all strings up to '190-200'
set(gca, 'xticklabel', strings)
根据您的数据,您可以使用:
x = [1 5.3 9 10.5 12 13 15.2 25 191 192.4];
nbins = round(max(x)/10+.5);
hist(x, nbins);
这是结果:
我试图让 数据成为 x 轴 并且 y 轴成为按 10[=27= 分组的值的数量].请参见下面的图作为示例
我尝试使用 bar(x),但我不确定您如何获得正确类别中的数字以进行绘图。
示例:
如果数据是 x=1,5.3,9,10.5,12,13,15.2,25,191,192.4
the group 0-10 should be 1,5.3,9
the group 10.1-20 should be 10.5,12,13,15.2
the group of 20.1-30 should be 25
.
.
.
the group of 190.1-200 should be 191,192.4
PS: 我使用的是 octave 3.8.1,它类似于 matlab
您可以使用 histc
。但是 histc
认为每个 bin 的左边缘相等,而不是右边缘:
bincounts = histc(x,binranges)
counts the number of values inx
that are within each specified bin range. The input,binranges
, determines the endpoints for each bin. The output,bincounts
, contains the number of elements fromx
in each bin.For example, if
binranges
equals the vector[0,5,10,13]
, thenhistc
creates four bins. The first bin includes values greater than or equal to0
and strictly less than5
. The second bin includes values greater than or equal to5
and less than10
, and so on. The last bin contains the scalar value13
.
要设置左边缘的相等条件,最好用 bsxfun
:
y = diff(sum(bsxfun(@le, x(:), 0:10:200), 1));
sum(bsxfun(...), 1)
查找 x
中有多少条目小于或等于 0
、10
、... 200
;然后 diff(...)
给出了想要的结果 y
:
y =
3 4 1 0 0 0 0 [...] 2
然后您可以使用 bar(y)
绘制条形图。如果您还想更改 x 轴上显示的文本,请设置轴的 'xticklabel'
属性:
bar(y);
strings = {'0-10', '10-20', '20-30'}; %// manually define all strings up to '190-200'
set(gca, 'xticklabel', strings)
根据您的数据,您可以使用:
x = [1 5.3 9 10.5 12 13 15.2 25 191 192.4];
nbins = round(max(x)/10+.5);
hist(x, nbins);
这是结果: