Info
此问题已关闭。 请重新打开它进行编辑或回答。
Hi, I need urgent help,the sum value in following program is not getting incremented..whatever 'm' value is there,sum is showing that.the next sum value is not getting added with the previous value.I here by attach the code.
1 次查看(过去 30 天)
显示 更早的评论
function [y,sum]= fcn(u,v)
%#codegen
w=length(u);
sum=0;
y=0;
for i=1:w
if u>v
m=u-v;
else m=0;
end
sum=sum+m;
if (u+sum)>v
y=v;
else
y=(u+sum);
end
end
2 个评论
Stephen23
2017-3-11
I am taking the value of u and v from outside.if u>v, I am storing the excess value in m, and giving output as "v".Each time this condition satisfies, I want to store the value of 'm' in 's'(which is the only problem I am having).
Then if u<v,"m"=0 it checks if "s+u">"v",if yes then output is" v "else "u+s"
modified code:
function [y,s]= fcn(u,v)
%#codegen
s=0;
if u>v
m=u-v;
else m=0;
end
s=s+m;
if (u+s)>v
y=v;
else y=(u+s);
end
this is also giving me the same output as previously.I am not getting any errors but i am not getting required output.
回答(1 个)
Image Analyst
2017-3-11
Do not use sum as the name of your variable. It is the name of a built-in function.
Step through your function with the debugger: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ so you can see what lines of code are, or are not , being executed.
Explain in words what you want to do. There's a good chance it can be accomplished in a vectorized manner instead of using for loops and if statements.
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!