简体   繁体   English

在矩阵Matlab中查找条件值

[英]Find conditional values in a matrix matlab

I have a matrix with individuals with a household id in the first column and their age in the second column: 我在第一列中有一个矩阵,其中有住户编号的个人,在第二列中有其年龄:

1  32
1  36
1  8
2  50
2  55
3  45
3  40
3  4
3  5
4  23

What I want to do is to find the number of persons between certain age classes with a specific household id. 我要做的是找到具有特定家庭ID的特定年龄段之间的人数。 An example of some of the age classes are seen below: 下面是一些年龄类别的示例:

0
1-2
3
4-6 
7-10
etc

So if I search household id 1 I want to find 1 in class 7-10 and when looking for household id 3 I want to find 2 in class 4-6. 因此,如果我搜索家庭ID 1我想在7-10类中找到1 ,而当寻找家庭ID 3我想在4-6类中找到2 How can I do this? 我怎样才能做到这一点?

histcounts counts the histogram, and you can input edges to it (the edges are the limits of each bin); histcounts计数直方图,您可以向其输入edges (边是每个bin的极限);

Full example on how to do this in 2 lines using your data, and random ranges. 有关如何使用数据和随机范围分两行执行此操作的完整示例。 The only thing you need to know is that whenever you want a single number as a bin, you just add the edges of the bin very very close to the value itself. 您需要知道的唯一一件事是,每当您希望将单个数字用作bin时,您只需将bin的边缘添加到非常接近值本身的位置即可。 In your case they are integers, so -0.1 +0.1 is enough. 在您的情况下,它们是整数,因此-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)

What you are looking for is surely a histogram. 您正在寻找的肯定是直方图。 However, I could not find a build-in function in MATLAB that supports zero-width bins as in your question. 但是,在您的问题中,我无法在MATLAB中找到支持零宽度垃圾箱的内置函数。 Therefore, I wrote a little script on how to compute the desired histogram manually: 因此,我写了一个有关如何手动计算所需直方图的小脚本:

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

Does this solve your problem? 这样可以解决您的问题吗?

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

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