How do I fix the code of my program
1 次查看(过去 30 天)
显示 更早的评论
[EDIT: 20110530 23:47 CDT - reformat - WDR]
Good morning everybody,
I have an error:
This code produces an error, and here is the error message:
Subscript indices must either be real positive integers or logicals.
Error in ==> vv at 15
mean(r)=sum(y(:,i))/400;
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> vv at 7
t=5*cos(2*pi*100*(0:1/300:1)+x);
My program is:
k=-1;
for n=1:400
k=k+1;
x(k+1)=k*(-2*pi/400);
end
x=x(:)';z=ones(1,301);
t=5*cos(2*pi*100*(0:1/300:1)+x);
l=0;
for j=1:400
l=l+1;
y(l,:)=[t+x(1,l).*z(1,:)];
end
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 realizations')
0 个评论
采纳的回答
Matt Fig
2011-5-31
I am not sure what you are trying to say about a mean value, but I think you are basically wanting to make this:
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));
Now w*t is along the columns and theta is along the rows of X. Also, to get a pretty plot I think you would need a higher time resolution, but I am not sure what you are trying to do beyond making X.
更多回答(1 个)
Walter Roberson
2011-5-31
Where do you initialize "r" ?
Please do not name a variable "mean": you will almost certainly encounter conflicts with the built-in function by that name.
With regards to "matrix dimensions must agree": your subexpression 2*pi*100*(0:1/300:1) will be a vector of length 301, and you try to add to that x, which is length 400. I do not know what you want to do so I cannot really suggest a solution... but it might involve bsxfun()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!