I can not figure out what's wrong with my code
1 view (last 30 days)
Show older comments
t = -2:0.01:4;
x = zeros(size(t));
x(find(t=-1 & t=1)) = 0.5*t+0.5;
x(find(t=1 & t=3)) = -0.5*t+1.5;
y = dirac(2);
z = conv(x,y);
plot(t,z);
I get this error message for line 3: "In an assignment A(I) = B, the number of elements in B and I must be the same."
1 Comment
Jan
on 7 Feb 2016
Please post the code with one command per line. On one hand this is easier to read. On the other hand it gets clear, which command caused the error.
Answers (2)
Jan
on 7 Feb 2016
Edited: Jan
on 9 Feb 2016
The line
x(find(t=-1 & t=1)) = 0.5*t+0.5;
fails, when the number of elements on the right and the left side differ. But in addition "t=-1" is an assignment, not a comparison. And finally it is impossible that t equals -1 and +1 at the same time. Perhaps you mean:
x(t==-1 | t==1) = 0.5 * t(t==-1 | t==1) + 0.5;
Note that an exact comparison is not meaningful with floating point numbers with a limited precision. See http://www.mathworks.com/matlabcentral/answers/57444-faq-why-is-0-3-0-2-0-1-not-equal-to-zero
0 Comments
Image Analyst
on 7 Feb 2016
This (t=-1 & t=1) is also not correct. You should use == and don't need find() at all. Perhaps you mean this:
t = -2:0.01:4;
x = zeros(size(t));
indexes = (t==-1 & t==1)
x(indexes) = 0.5*t(indexes) + 0.5;
indexes = (t==-1 & t==1)
x(indexes) = -0.5*t(indexes) + 1.5;
y = dirac(2);
z = conv(x,y);
plot(t,z);
But I don't have the dirac() function and don't really know what you want to do.
0 Comments
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!