I have a vector and want to compute the average value

4 次查看(过去 30 天)
[EDIT: 20110604 17:36 CDT - reformat - WDR]
Good afternoon,
This is my program and teacher Matt helped me but the result of my program should appear like the result of the main program This is my program but my graph result is different from the main program because the mean value must be zero or near to zero
Please, anyone help me
The Main program and I want my graph result appear like the graph result of main program
k=-1;
for n=1:400
k=k+1;
x(k+1)=k*(-2*pi/400);
end
x=x(:)';
s=ones(1,301);
z=0;
for j=1:400
z=z+1;
y(z,:)=5*cos(2*pi*100*(0:1/300:1)+x(1,z).*s(1,:));
end
r=0;
for i=1:301
r=r+1;
mean(r)=sum(y(:,i))/400;
end
plot(mean,'o'),grid
title('The Mean of the signal')
ylabel('Mean')
xlabel('Number of points')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
My program
theta = linspace(-2*pi,2*pi,400); % 400 equally spaced points
t = linspace(0,1,301); % 301 equally spaced points between 0 and 1.
w = 2*pi*100;
A = 5;
X = A*cos(bsxfun(@plus,theta.',w*t));
r=0;
for i=1:301
r=r+1;
mean(r)=sum(X(:,i))/400;
end
plot(mean,'o'),grid
title('The Mean of the signal')
ylabel('Mean')
xlabel('Number of points')
  3 个评论
reem
reem 2011-6-4
Thanks a lot jan
but I do not have any problem in my program,I put two programs here,the first program is ((Main program))it and t((my program))it is the second program,and these two programs have the same ideas and the result of the graph must be appear similar,so my question is why my program result not appear like the result of the first program((main program)).
Jan
Jan 2011-6-4
@Reem: It would be polite to make the reading of your question as easy as possible.
It is obvious that the two versions calculate different numbers. But due to the strange formatted code, the comparison of the two programs is difficult. If you concentrate to the essential lines, we could see both versions without scrolling and the difference would be *much* easier to see - and therefore to find.

请先登录,再进行评论。

采纳的回答

Matt Fig
Matt Fig 2011-6-4
First of all, don't name a variable mean! This will mess up your use of the MATLAB function MEAN. This produces a plot similar to your main program, but there will be slight differences due to different approaches.
clear mean % Get rid of this bad variable name!!
theta = linspace(-2*pi*399/400,0,400); % 400 equally spaced points
t = linspace(0,1,301); % 301 equally spaced points between 0 and 1.
w = 2*pi*100;
A = 5;
X = A*cos(bsxfun(@plus,theta.',w*t));
mn2(r) = mean(X); % No need for a loop, use the MATLAB function
figure
plot(mn2,'o'),grid
title('The Mean of the signal')
ylabel('Mean')
xlabel('Number of points')
  1 个评论
reem
reem 2011-6-4
Thanks a lot My best teacher Matt
you are very good teacher for us,I respect you a lot.
once again,Thank you so much for your help

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by