How do i count unique elements in two table columns?

Dear all;
for the following table:which include student name, class and school name ..
std_name class School
A1 1 Art
A2 1 Art
A3 1 Muc
A4 2 muc
A5 3 Art
A6 2 fun
A7 3 fun
A8 4 Muc
...
i need to count : - how many student in every class, then write new table includes the following result as example: class number and how many student in every class, then names of schools... as shown below:
class No. of std School
1 50 Art, Muc
2 30 muc, fun
3 10 Art, fun
4 100 muc
etc..
-

 채택된 답변

Guillaume
Guillaume 2017년 6월 9일
편집: Guillaume 2017년 6월 9일
Simpler, using rowfun which did exist in R2015a:
out = rowfun(@(school) {strjoin(unique(school), ', ')}, A, ...
'GroupingVariables', 'class', 'InputVariables', 'School')

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 6월 9일
편집: Andrei Bobrov 2017년 6월 9일
A = readtable('A.csv');
A.Properties.VariableNames = {'std_name','class','School'};
[g,ii] = findgroups(A.class);
out = table(ii,accumarray(g,1),splitapply(@(x){strjoin(unique(x),',')},A.School,g),...
'var',{'class','No_of_std','School' });

댓글 수: 5

  • Undefined function or variable 'findgroups'??
  • Undefined function or variable 'splitapply'??
i'm using matlab 2015.. thanks
Both of those functions were introduced in R2015b.
this is my installing directory... is there any difference between a and b? matlab version..
\MATLAB Production Server\R2015a
i would thank any one can gibe me link to download it..
Guillaume
Guillaume 2017년 6월 9일
편집: Guillaume 2017년 6월 9일
You download R2015b like you download any other releases, directly from the mathworks site:
But if you're going to upgrade, you'd be better off moving to the latest version: 2017a
Perhaps ... modified code to earlier version.. upgrades is not available currently.. thanks

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Downloads에 대해 자세히 알아보기

제품

태그

질문:

2017년 6월 9일

댓글:

2017년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by