我有一个矩阵,其中第一列中有家庭ID的个人和第二列中的年龄:
1 32
1 36
1 8
2 50
2 55
3 45
3 40
3 4
3 5
4 23
我想要做的是找出具有特定家庭ID的某些年龄组之间的人数。下面是一些年龄组的例子:
0
1-2
3
4-6
7-10
etc
因此,如果我搜索家庭ID 1
,我想在7-10级中找到1
,在寻找家庭ID 3
时,我想在课堂上找到2
4-6。我怎么能这样做?
答案 0 :(得分:1)
histcounts
计算直方图,你可以输入edges
(边是每个bin的限制);
如何使用您的数据和随机范围在2行中完成此操作的完整示例。您唯一需要知道的是,无论何时您想要一个数字作为bin,您只需将bin的边缘非常接近于值本身。在你的情况下,它们是整数,所以-0.1 +0.1就足够了。
M=[1 32
1 36
1 8
2 50
2 55
3 45
3 40
3 4
3 5
4 23];
ranges=[-0.1 0.1 1 4 20 30 40]; % random
% separate the matrix into cells with unique IDs
Mcell = arrayfun(@(x) M(M(:,1) == x, :), unique(M(:,1)), 'uniformoutput', false);
% Count bins in each cell
result=cellfun(@(x)(histcounts(x(:,2),ranges)),Mcell,'uniformoutput',false)
答案 1 :(得分:1)
您正在寻找的肯定是直方图。但是,我无法在MATLAB中找到支持零宽度分档的内置函数。因此,我写了一个关于如何手动计算所需直方图的小脚本:
% the age data
% (first column: id
% second column: age)
mat_data = [1 32
1 36
1 8
2 50
2 55
3 45
3 40
3 4
3 5
4 23];
% the age classes
% (one class per row,
% lower bound in first column,
% upper bound in second column)
age_classes = [0, 0; ...
1, 2; ...
3, 3; ...
4, 6; ...
7, 10];
% determine all ids
ids = unique(mat_data(:, 1));
% initialize the output matrix
mat_counts = zeros(size(age_classes, 1), length(ids));
% count the number of entries for each id and age class
for idx_id = 1 : length(ids)
cur_data = mat_data(mat_data(:, 1) == ids(idx_id), 2);
for idx_age_class = 1 : length(age_classes)
cur_age_class = age_classes(idx_age_class, :);
mat_counts(idx_age_class, idx_id) = length(find(cur_data >= cur_age_class(1) & cur_data <= cur_age_class(2)));
end
end
% plot everything
% create the x-axis labels
xticks = {};
for idx_age_class = 1 : length(age_classes)
if length(unique(age_classes(idx_age_class, :))) == 1
xticks{idx_age_class} = num2str(age_classes(idx_age_class, 1));
else
xticks{idx_age_class} = sprintf('%.0f-%.0f', age_classes(idx_age_class, 1), age_classes(idx_age_class, 2));
end
end
figure(1);
bar(mat_counts);
set(gca, 'xticklabel', xticks);
legend(cellfun(@num2str, num2cell(ids)))
xlabel('age class')
ylabel('count')
这会解决您的问题吗?