Count unique categorical values in table
이전 댓글 표시
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
2014년 10월 25일
What is tNonNumeric? what type of variable?
채택된 답변
추가 답변 (2개)
Image Analyst
2014년 10월 25일
0 개 추천
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
Ege
2014년 10월 25일
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.
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
2014년 10월 25일
Image Analyst
2014년 10월 25일
I don't even see cat in Azzi's code. What are you doing?
Ege
2014년 10월 25일
Azzi Abdelmalek
2014년 10월 25일
To make things easier, post a sample of your data
Image Analyst
2014년 10월 25일
If they're strings, the sizes are probably different so you'll have to create a cell array instead of a character array, which must be rectangular. See http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
Ege
2014년 10월 25일
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?
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
