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!