MATLAB program which computes the mean and standard deviation of the data. Use FOR LOOPs.
Posted in matlab
No comments
Thursday, August 7, 2014 By Anonymous
We
can generate a set of data with a random values about some
mean
value with the command
num=100;
spr=0.1;
data=5.0+spr*sign(1.0-2.0*rand(1,num)).*rand(1,num)
a) What is the last line doing?
b)
Write a MATLAB program which computes the mean and standard deviation of the data.
Use FOR LOOPs.
Solution:
function Question9()
num = 100;
spr = 0.1;
data = 5.0 + spr * sign(1.0 - 2.0 *
rand(1,num)) .* rand(1,num); %according
to as per given
Total=0;
Size = size(data);
for i=1 : Size(2)
Total = Total + data(i);
end
Mean = Total/num; %getting mean
Sum = 0;
for i=1 : Size(2)
Sum = Sum + ((data(i) - Mean) ^ 2);
end
SD = sqrt(Sum / (num-1));
fprintf('Mean of
Data: %.2f \n', Total/num);
fprintf('SD of data
%.2f \n', SD); %printig output
end
Related posts
Share this post
0 comments: