why is there no output?

2 次查看(过去 30 天)
Lee
Lee 2013-5-5
im suppose to create a function that gets a vector x of 10 elements and returns
a new vector with 10 elemnts.the new vector component (i) is e if i was bigger then 1 1/e if i was smaller then zero and e^(2*x(1,i)-1) if i was between 0 and 1
this is what i did for some reason i get no return what am i doing wrong?
function targil7=targil(x)
z=length(x);
w=zeros(1,10);
for i=1:z
if x(1,i)>1
w(1,i)=w(1,i)+exp(1);
else if x(1,i)<0
w(1,i)=w(1,i)+exp(-1);
else if 0<x(1,i)<1
w(1,i)=w(1,i)+exp(2*(x(1,i)-1));
end
end
end
end
targil7=w;
whats am i doing wrong?

回答(2 个)

Walter Roberson
Walter Roberson 2013-5-5
if 0<x(1,i)<1
is not correct. To MATLAB it means ((0<x(1,i)) < 1) which means to compute the logical result of the comparison to 0, getting a 0 or 1 as a result, and then to test that value to see if it is less than 1. Try
if 0 < x(1,i) & x(1,i) < 1
Note: when you do that, you need to figure out what you want to do if that test is not true either. If there are no circumstances under which it could be false at that point, then skip the test and just do the assignment.

Azzi Abdelmalek
Azzi Abdelmalek 2013-5-5
Maybe, you are not calling your function correctly
v=1:10
out=targil(v)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by