Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

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 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 11 Mar 2017
Smruti Prasad Dash's "Answer" moved here:
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.

Answers (1)

Image Analyst
Image Analyst on 11 Mar 2017
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.

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!