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에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!