フィルターのクリア

Count unique categorical values in table

28 ビュー (過去 30 日間)
Ege
Ege 2014 年 10 月 25 日
回答済み: Mohammad Abouali 2014 年 10 月 25 日
My table consist of only categorical values. I try to find unique elements and how many times do they appear in my table. Not my actual data but following can be used instead:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
FirstName = {'Amanda' ;'Brenda';'Carl'; 'Denis'; 'Ethan'};
Something = {'String1' ;'String2';'String2'; 'String1'; 'String5'};
Weight = [176;163;131;133;119];
FavoriteColor = {'blue' ;'red' ;'yellow'; 'orange' ;'colorblind' };
T = table(Age,FirstName,Weight,FavoriteColor,Something,'RowNames',LastName)
T.FavoriteColor= categorical(T.FavoriteColor);
T.Something= categorical(T.Something);
I think It is similar to the idea of applying a histogram function so I tried it as well but the data being categorical makes it hard. Could you help?
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 25 日
What is tNonNumeric? what type of variable?
Ege
Ege 2014 年 10 月 25 日
編集済み: Ege 2014 年 10 月 25 日
tNonNumeric is a table which has categorical values in it.

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

採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 10 月 25 日
something like this:
First your code to generate sample data:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
FirstName = {'Amanda' ;'Brenda';'Carl'; 'Denis'; 'Ethan'};
Something = {'String1' ;'String2';'String2'; 'String1'; 'String5'};
Weight = [176;163;131;133;119];
FavoriteColor = {'blue' ;'red' ;'yellow'; 'orange' ;'colorblind' };
T = table(Age,FirstName,Weight,FavoriteColor,Something,'RowNames',LastName)
T.FavoriteColor= categorical(T.FavoriteColor);
T.Something= categorical(T.Something);
now getting the unique values
unqValue=unique(T.Something);
Now counting
n=arrayfun(@(x) sum(T.Something==x),unqValue);
Now plotting
bar(n)
Now first changing the unqValue from categorical to Cell array and then adding it as labels to the bar chart
unqValueCell=arrayfun(@(x) char(x(1)),unqValue,'UniformOutput',false);
set(gca,'XTickLabel',unqValueCell)
Here is the bar chart:

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 10 月 25 日
For some reason you chose to use the original poster's code which he said wasn't working rather than either of the two answers that worked. Why? Why not try to use one of the scripts that worked?
  3 件のコメント
Image Analyst
Image Analyst 2014 年 10 月 25 日
What he needed was the histogram. Both answers had that but his original code did not . You can use one of the Answers or try Azzi's code here.
Ege
Ege 2014 年 10 月 25 日
編集済み: Ege 2014 年 10 月 25 日
Ok I figured the histogram too and Azzi's answer works well for numerical values but if we add a categorical column to it it gives error.
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
FirstName = {'Amanda' ;'Brenda';'Carl'; 'Denis'; 'Ethan'};
Something = {'String1' ;'String2';'String2'; 'String1'; 'String5'};
Weight = [176;163;131;133;119];
FavoriteColor = {'blue' ;'red' ;'yellow'; 'orange' ;'colorblind' };
T = table(Age,FirstName,Weight,FavoriteColor,Something,'RowNames',LastName)
T.FavoriteColor= categorical(T.FavoriteColor);
T.Something= categorical(T.Something);
a=T.Something
[uniq,jj,kk]=unique(a)
freq=histc(kk,1:numel(uniq))
out=[uniq freq]
the result:
a =
'String1'
'String2'
'String2'
'String1'
'String5'
uniq =
'String1'
'String2'
'String5'
jj =
1
2
5
kk =
1
2
2
1
3
freq =
2
2
1
Error using horzcat Dimensions of matrices being concatenated are not consistent.

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


Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 25 日
Look at this example
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;40];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
tNonNumeric = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)
%---------------------------------------------------------------------------
a=tNonNumeric.Age
[uniq,jj,kk]=unique(a)
freq=histc(kk,1:numel(uniq))
out=[uniq freq]
  7 件のコメント
Ege
Ege 2014 年 10 月 25 日
I edited the question and added some sample data hope that helps
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 25 日
What is your question for the new example? what column? the first? or all the columns at the same time?

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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by