Select groups of data with a specific size

1 回表示 (過去 30 日間)
Maria Granell Ruiz
Maria Granell Ruiz 2019 年 3 月 18 日
コメント済み: Maria Granell Ruiz 2019 年 3 月 18 日
Dear all,
Imagine you have a matrix with three colums, the first one is a grouping variable and the other two are coordinates (xy).
The grouping variable looks like this
A = [1 1 1 1 2 2 2 3 3 3 3]';
In this way i have four coordinates in the group 1, three coordinates in the group 2 and four in the group 3. I would like to exclude the groups that do not have 4 data points, i.e., more or less than 4 (in this case group 2).

採用された回答

Guillaume
Guillaume 2019 年 3 月 18 日
Simply build the histogram of your groups (with histcounts or accumarray) and select the groups you want to keep with ismember:
demodata = [[1 1 1 1 2 2 2 3 3 3 3]', randi(100, 11, 2)]
[group, ~, groupid] = unique(demodata(:, 1));
groupcount = accumarray(groupid, 1); %histogram of groups
keptdata = demodata(ismember(demodata(:, 1), group(groupcount == 4)), :)
  1 件のコメント
Maria Granell Ruiz
Maria Granell Ruiz 2019 年 3 月 18 日
Thank you so much! That was exactly what i needed :)

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

その他の回答 (1 件)

KSSV
KSSV 2019 年 3 月 18 日
Let x,y be your data points
A = [1 1 1 1 2 2 2 3 3 3 3]';
for i = 1:max(X)
idx = A==i ;
if nnz(idx)<3
plot(x(idx),y(idx),'.')
end
end
  1 件のコメント
Maria Granell Ruiz
Maria Granell Ruiz 2019 年 3 月 18 日
Thank you for you quick answer!
But when i run your code it creates a logical vector idx that "selects" group 3, so not what i want. I would like to select the groups with four entries, no more or less.
you can use this imaginary coordinates:
xy = [1,2;3,4;4,5;5,6;2,3;4,5;6,7;7,8;3,4;8,3;4,9];

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

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by