필터 지우기
필터 지우기

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
Stephen23 2017년 3월 11일
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.

답변 (1개)

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

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by