根据条件绘制排除某些类别的组变量

时间:2018-11-23 09:20:22

标签: matlab histogram categorical-data

我想绘制一个组变量的直方图。为此,我可以使用分类。

我正在使用一个示例,使用summary(Group)来解释:

variable.group={'one','three','four','one','three','four','two','three','four','one','three','four','one','three','four','one','three','four','one','three','four','one','three','four','one','three','four','one','three','four'}
Group=categorical(variable.group)
summary(Group)
figure,histogram(Group),title('Summary Group')

enter image description here

我也可以使用tabulate(Group)来获得以下结果:

Group_tabulated =
  4×3 cell array
    'four'     [10]    [33.3333]
    'one'      [ 9]    [     30]
    'three'    [10]    [33.3333]
    'two'      [ 1]    [ 3.3333]

现在,正如我们在上图中所看到的,有一个小组很少出现;我想排除该类别,以专注于最重要的3个类别。

现在使用制表条件,我几乎可以做到这一点。但是我有一个问题,因为我要排除的类别仍在显示……现在是0。

Group_tabulated = tabulate(Group)
idx_largest=cell2mat(Group_tabulated(:,2))>3
Group_to_display=Group_tabulated(idx_largest,1)
Learn1_1n_largest=Group(ismember(Group,Group_tabulated(idx_largest,1)))
summary(Learn1_1n_largest)
figure,histogram(Learn1_1n_largest),title('Summary Group largest only')

enter image description here

2个问题

  1. 我们可以通过列表使解决方案生效,以便从Learn1_1n_largest中排除类别数据吗?
  2. 为此,我可以在分类或直方图上使用某些条件吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了第一个问题的解决方案。可以使用removecats来删除所有未使用的类别。只需不指定类别Learn1_1n_largest=removecats(Learn1_1n_largest)这样的分类即可。我第一个问题的完整解决方案是:

variable.group={'one','three','four','one','three','four','two','three','four','one','three','four','one','three','four','one','three','four', ...
    'one','three','four','one','three','four','one','three','four','one','three','four'}
Group=categorical(variable.group)
summary(Group)
figure,histogram(Group),title('Summary Group')
Group_tabulated = tabulate(Group)
idx_largest=cell2mat(Group_tabulated(:,2))>3
Group_to_display=Group_tabulated(idx_largest,1)
Learn1_1n_largest=Group(ismember(Group,Group_tabulated(idx_largest,1)))
figure,histogram(Learn1_1n_largest),title('Summary Group largest only')
Learn1_1n_largest=removecats(Learn1_1n_largest)
figure,histogram(Learn1_1n_largest),title('Summary Group largest only')

如果有人能解决我的第二个问题,我将感到很高兴,因为我希望可以有一个更简单的解决方案,而不必同时使用分类和制表。

谢谢!