how to calculate median of grouped data in MATLAB

15 visualizaciones (últimos 30 días)
N/A
N/A el 20 de Jun. de 2024
Respondida: Pavan Sahith el 24 de Jun. de 2024
Given the two examples calculate the median in MATLAB using the function median
Example 1
Example 2
First example answer is 450 and the answer to the example 2 is 25.25

Respuesta aceptada

Pavan Sahith
Pavan Sahith el 24 de Jun. de 2024
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;
expanded_data = [];
for i = 1:length(frequencies)
expanded_data = [expanded_data, repmat(midpoints(i), 1, frequencies(i))];
end
median_value = median(expanded_data);
disp(['The median is: ', num2str(median_value)]);
The median is: 27.5
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];
% Calculate the median using the custom function
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)
% Calculate cumulative frequency
cum_frequencies = cumsum(frequencies);
% Total number of observations
N = sum(frequencies);
% Find the median class (first class where cumulative frequency >= N/2)
median_class_index = find(cum_frequencies >= N/2, 1);
% Extract the median class boundaries and frequency
L = class_intervals(median_class_index, 1);
f = frequencies(median_class_index);
CF = cum_frequencies(median_class_index - 1);
if isempty(CF)
CF = 0;
end
h = class_intervals(median_class_index, 2) - L;
% Calculate the median
median_value = L + ((N/2 - CF) / f) * h;
end
You can also try referring to these file exchange functions which might help you
I hope this helps you moving forward

Más respuestas (1)

Muskan
Muskan el 24 de Jun. de 2024
Calculating the median of grouped data involves estimating the median from a frequency distribution table. Ypu can follow the following steps:
  1. Organize Data: Ensure you have the class intervals and the corresponding frequencies.
  2. Calculate Cumulative Frequencies: Compute the cumulative frequency for each class interval.
  3. Identify the Median Class: Find the class interval where the median lies.
  4. Apply the Median Formula: Use the formula for the median of grouped data.
The formula for the median of grouped data is:
Where:
  • ( L ) = Lower boundary of the median class
  • ( N ) = Total number of observations
  • ( CF ) = Cumulative frequency of the class preceding the median class
  • ( f ) = Frequency of the median class
  • ( h ) = Class width
I hope this helps!

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by