Hello NAFISA,
I see that you are trying to calculate the median of grouped data using MATLAB's median function. However, the 'median' function in MATLAB is designed for raw data inputs and does not directly compute the median for grouped data.
you can expand your grouped data as follows:
class_intervals = [0 5; 5 10; 10 15; 15 20; 20 25; 25 30; 30 35; 35 40; 40 45; 45 50];
frequencies = [14, 8, 20, 7, 11, 10, 5, 16, 21, 9];
midpoints = (class_intervals(:, 1) + class_intervals(:, 2)) / 2;
for i = 1:length(frequencies)
expanded_data = [expanded_data, repmat(midpoints(i), 1, frequencies(i))];
median_value = median(expanded_data);
disp(['The median is: ', num2str(median_value)]);
Using this method, you will find that the output is 27.5 instead of the expected 25.25. This discrepancy occurs because:
- When you expand grouped data into individual data points, you assume that all data points within a class interval are located at the midpoint of that interval.
- For example, if a class interval is [20, 25] with a frequency of 11, you assume there are 11 data points all exactly at 22.5.
- This assumption can lead to inaccuracies because the actual data points could be spread across the entire interval [20, 25].
So, as Muskan mentioned , you can use the grouped data median formula (L + ((N/2 - cf) / f) * h).
If you frequently work with frequency distribution tables and find it cumbersome to use this formula manually, you can use MATLAB functions to simplify the process. Here is an example:
class_intervals1 = [0 5; 5 10; 10 15; 15 20; 20 25; 25 30; 30 35; 35 40; 40 45; 45 50];
frequencies1 = [14, 8, 20, 7, 11, 10, 5, 16, 21, 9];
class_intervals2 = [420 430; 430 440; 440 450; 450 460; 460 470; 470 480; 480 490; 490 500];
frequencies2 = [336, 2112, 2336, 1074, 1553, 1336, 736, 85];
median_value1 = groupedMedian(class_intervals1, frequencies1);
median_value2 = groupedMedian(class_intervals2, frequencies2);
disp(['The median of the grouped data1 is: ', num2str(median_value1)]);
The median of the grouped data is: 25.25
disp(['The median of the grouped data2 is: ', num2str(median_value2)]);
The median of the grouped data is: 450
function median_value = groupedMedian(class_intervals, frequencies)
cum_frequencies = cumsum(frequencies);
median_class_index = find(cum_frequencies >= N/2, 1);
L = class_intervals(median_class_index, 1);
f = frequencies(median_class_index);
CF = cum_frequencies(median_class_index - 1);
h = class_intervals(median_class_index, 2) - L;
median_value = L + ((N/2 - CF) / f) * h;
You can also try referring to these file exchange functions which might help you
I hope this helps you moving forward