median of grouped frequency data
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Given the data below
calculate the median of the data in MATLAB
I have the code below
class_intervals = [420 430; 430 440; 440 450; 450 460; 460 470; 470 480; 480 490; 490 500];
frequencies = [336, 2112, 2336, 1074, 1553, 1336, 736, 85];
% Calculate the cumulative frequencies
cum_frequencies = cumsum(frequencies)
% Calculate the total number of observations
N = sum(frequencies);
% Find the median class (where cumulative frequency exceeds N/2)
median_class_index = find(cum_frequencies >= N/2, 1)
% Extract the lower boundary, frequency, and cumulative frequency of the class before the median class
L = class_intervals(median_class_index, 1) % lower boundary of the median class
f = frequencies(median_class_index) % frequency of the median class
if median_class_index == 1
cf = 0; % cumulative frequency before the median class
else
cf = cum_frequencies(median_class_index - 1) % cumulative frequency before the median class
end
% Calculate the width of the median class interval
h = class_intervals(median_class_index, 2) - class_intervals(median_class_index, 1)
% Calculate the median using the formula
median = L + ((N/2 - cf) / f) * h
However I was wondering if instead of calculating it manually is there any inbuilt function in MATLAB to calculate median of grouped data.
0 comentarios
Respuestas (1)
Anurag Ojha
el 11 de Jun. de 2024
Hi Nafisa
You can use 'grpstats' function in order to calculate median of a grouped data. Adding a pseudo code:
% Create a table with the class intervals and frequencies
data = table(class_intervals, frequencies, 'VariableNames', {'ClassIntervals', 'Frequencies'});
% Calculate the median using grpstats
median_data = grpstats(data, [], 'median', 'DataVars', 'ClassIntervals');
% Extract the median values
median_values = median_data.median_ClassIntervals;
% Display the median values
disp(median_values)
There is also a inbuilt 'Median' function that you can use to caluclate median.
data = [10, 20, 30, 40, 50];
median_value = median(data);
disp(median_value);
Adding documentation link for your reference:
5 comentarios
Anurag Ojha
el 13 de Jun. de 2024
Editada: Anurag Ojha
el 13 de Jun. de 2024
Hi Nafisa
I am not getting this error at my end when I execute the provided code. Can you please provide the exact code that you are executing which results in this error.
Ver también
Categorías
Más información sobre Measurements and Spatial Audio en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!