How do I fix the code of my program

1 view (last 30 days)
reem
reem on 31 May 2011
[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')

Accepted Answer

Matt Fig
Matt Fig on 31 May 2011
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 Comment
reem
reem on 31 May 2011
firstly,Thanks a lot my teacher Matt for your help
My program idea is:-
To compute mean value 1)I must compute the summation of all values X(t) for matrix of size 400*301 and after computing the summation for all value of X(t) inside the loop where i=1:400 and j=1:301 and after that I must divide the value of summation of all X(t) on 400 and this is mean value((average)) and then I must plot this mean value but all the problem related in my loop(i.e the size of matrix))
also when I compute X(t) for each row and column all value are the same and only the value of theta is changed because it is increment by 1 every time inside the loop
this is what I trying to do

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 31 May 2011
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()

Community Treasure Hunt

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

Start Hunting!