0% found this document useful (0 votes)
4 views4 pages

Task Code-1

The MATLAB code reads ungrouped numerical data from a text file and calculates various statistical measures including the arithmetic mean (AM), geometric mean (GM), harmonic mean (HM), median, mode, variance, standard deviation, and moments about the mean. It also handles cases where there is no mode and outputs the results to the console. The code includes sorting of the data for median and mode calculations.

Uploaded by

sohan2203021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Task Code-1

The MATLAB code reads ungrouped numerical data from a text file and calculates various statistical measures including the arithmetic mean (AM), geometric mean (GM), harmonic mean (HM), median, mode, variance, standard deviation, and moments about the mean. It also handles cases where there is no mode and outputs the results to the console. The code includes sorting of the data for median and mode calculations.

Uploaded by

sohan2203021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MATLAB Code — task.

clc
clear
close all

fid = fopen('[Link]','r');
data = fscanf(fid,'%f');
fclose(fid);

disp('Ungrouped Data:')
disp(data')

n = length(data);

sum_data = 0;
for i = 1:n
sum_data = sum_data + data(i);
end

AM_ungroup = sum_data/n;

log_sum = 0;
for i = 1:n
log_sum = log_sum + log(data(i));
end

GM_ungroup = exp(log_sum/n);

sum_inv = 0;
for i = 1:n
sum_inv = sum_inv + (1/data(i));
end

HM_ungroup = n/sum_inv;

for i = 1:n-1
for j = 1:n-i
if data(j) > data(j+1)
temp = data(j);
data(j) = data(j+1);
data(j+1) = temp;
end
end
end

if mod(n,2) == 0
median_ungroup = (data(n/2) + data(n/2 + 1))/2;
else
median_ungroup = data((n+1)/2);
end

max_count = 0;
mode_values = [];

for i = 1:n
count = 0;

for j = 1:n
if data(i) == data(j)
count = count + 1;
end
end

if count > max_count


max_count = count;
mode_values = data(i);

elseif count == max_count


if ~ismember(data(i), mode_values)
mode_values = [mode_values data(i)];
end
end
end

if max_count == 1
no_mode_flag = true;
else
no_mode_flag = false;
end

sum_sq = 0;

for i = 1:n
sum_sq = sum_sq + (data(i) - AM_ungroup)^2;
end
var_ungroup = sum_sq/n;
sd_ungroup = sqrt(var_ungroup);

moment1 = 0;
moment2 = 0;
moment3 = 0;
moment4 = 0;
moment5 = 0;

for i = 1:n
moment1 = moment1 + (data(i)-AM_ungroup)^1;
moment2 = moment2 + (data(i)-AM_ungroup)^2;
moment3 = moment3 + (data(i)-AM_ungroup)^3;
moment4 = moment4 + (data(i)-AM_ungroup)^4;
moment5 = moment5 + (data(i)-AM_ungroup)^5;
end

moment1 = moment1/n;
moment2 = moment2/n;
moment3 = moment3/n;
moment4 = moment4/n;
moment5 = moment5/n;

fprintf('\n\n UNGROUPED DATA \n')

fprintf('\nMean:\n')
fprintf('AM = %.2f\n',AM_ungroup)
fprintf('GM = %.2f\n',GM_ungroup)
fprintf('HM = %.2f\n',HM_ungroup)

fprintf('\nMedian = %.2f\n',median_ungroup)

if no_mode_flag
fprintf('Mode = No Mode (all values appear once)\n');
else
fprintf('Mode = ');
fprintf('%.2f ', mode_values);
fprintf('(appears %d times)\n', max_count);
end

fprintf('\nVariance = %.2f\n',var_ungroup)
fprintf('SD = %.2f\n',sd_ungroup)
fprintf('\nMoments about Mean\n')
fprintf('1st Moment = %.2f\n',moment1)
fprintf('2nd Moment = %.2f\n',moment2)
fprintf('3rd Moment = %.2f\n',moment3)
fprintf('4th Moment = %.2f\n',moment4)
fprintf('5th Moment = %.2f\n',moment5)

You might also like