Clear Filters
Clear Filters

New to Matlab. I need help with group (?), double counting, and so on. Thank you!

1 view (last 30 days)
New to Matlab, I need your help.
Suppose my data is this:
schoolid classid classsize enrollment
1001 1 10 22
1001 2 12 22
1005 1 14 29
1005 2 15 29
1018 1 8 22
1018 2 4 22
1018 3 10 22
.
.
.
And I want to find:
a. number of schools.
I found this one by
grps = findgroups(schoolid);
n = max(grps);
b. the average number of classes in a school.
I know how to solve by hand, but you know, my data is big,,, help me with codes please.
c. Provide the histogram of enrollment, and report its mean, median, max and min.
I know codes to find histogram, mean, and so on,
but you see the enrollemnt column is a double counting, I need to figure this out frist.
Please help me... Matlab geniuses... Thank you!

Accepted Answer

Image Analyst
Image Analyst on 4 Jun 2020
For a, and alternate way is
numberOfSchools = length(unique(schoolid))
A for loop way for b and c is
us = unique(schoolid)
numRows = length(schoolid)
for k = 1 : length(us)
thisSchoolsRows = schoolid == us(k); % Only rows with this particular school
numClasses(k) = length(thisSchoolsRows) % Assumes each classid's are unique and show up in only one row for a particular school.
end
aveClassesOverAllSchools = numClasses(k) / numberOfSchools
You can use the same approach to compute your other statistics. You can also use grpstats() or splitapply() like Rik mentioned.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!