Why is the for loop not updating y matrix??
Show older comments
f=10000;
tmax=.01;
t=0:1/f:tmax;
[m n]=size(t);
y=randperm(n);
a=sin(2*pi*400*t);
b=sin(2*pi*400*t+pi/2);
f=a>0;
g=b>0;
pr=circshift(g,[1 n-1]);
subplot(5,1,1);
plot(t,a),grid on;
subplot(5,1,2);
plot(t,b),grid on;
subplot(5,1,3);
plot(t,f),grid on;
subplot(5,1,4);
plot(t,g),grid on;
for i=2:size(t),
if pr(1,i)~=g(1,i)
y(1,i)=xor(f(1,i),pr(1,i));
else
y(1,i)=y(1,i-1);
end
end
subplot(5,1,5);
plot(t,y);
I'm trying to updated y matrix if a condition is satisfied. But it is not happening so. Why?
Could someone help me??
Thank you!!
Answers (1)
Walter Roberson
on 4 Feb 2012
Your line
for i=2:size(t)
is wrong. size(t) returns a vector, and the colon operator does strange things when it is given a vector.
Change the size(t) to length(t)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!