I can not figure out what's wrong with my code

1 次查看(过去 30 天)
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 个评论
Jan
Jan 2016-2-7
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.

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2016-2-7
编辑:Jan 2016-2-9
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

Image Analyst
Image Analyst 2016-2-7
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.

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by