Imbedded If statement in Function help!

Hi all!
I am trying to write a function that allows me to add up all of my inputs given the conditions in my code. I am trying to make it so I can add it up to see what the help index is. I'm pretty sure my function code is wrong. If you can help tell me where I can fix it I would greatly appreciate it! Thank you! in advance
function [helpIndex]=needsHelp(age,duration,burden,sati); %function to calculate the helpIndex using age(yrs),duration(months),burden,and satisfaction
if age>50 % If age is greater than 50 add 1 point
helpIndex=n+1
elseif duration>60% If duration is greater than 60 add 1 point
helpIndex=n+1
elseif burden>70% If burden is greater than 70 add 1 point
helpIndex=n+1
elseif burden>70 && sati<50 %Then if burden is greater than 70 and satisfaction is less than 50 add an additional point.
helpIndex=n+1
end

2 个评论

n is not defined in the function.
Here is an excellent video course that shows you how to program - no experience necessary. They use MATLAB as the programming language (also, no experience necessary). You can take the free version by hitting the audit button.

请先登录,再进行评论。

回答(1 个)

Did you know that
>> logical(1) + logical(1)
ans =
2
and that
(age>50)
is a logical?

5 个评论

Hi Paul,
No I didn't know that.
For me to format it like that then would I just have to do it like this, or how should I go about changing it for that to work? Thank you for all of your help!
function [helpIndex]=needsHelp(age,duration,burden,sati); %function to calculate the helpIndex using age(yrs),duration(months),burden,and satisfaction
if age>50 % If age is greater than 50 add 1 point
elseif duration>60% If duration is greater than 60 add 1 point
elseif burden>70% If burden is greater than 70 add 1 point
elseif burden>70 && sati<50 %Then if burden is greater than 70 and satisfaction is less than 50 add an additional point.
helpIndex=(age>50)+(duration>60)+(burden>70)+(burden>70 && sati<50)
end
I probably should have asked you for what you want helpIndex to be for a given set of inputs. For example, here is my assumption:
age = 65; % add 1 point
duration = 44; % add 0 point
burden = 75; % add 1 point
sati=12; % add 1 point
% with the above values, the helpIndex should be 3.
Is this assumption correct? If this value of 3 is what you are looking for, then the code:
helpIndex=(age>50)+(duration>60)+(burden>70)+( (burden>70) && sati<50)
seems to get the right answer. I just added an extra parens in the last term. You can change the values around and make sure that you always get the answer you are expecting. If you are, then what is the purpose of your if-elseif expressions?
The reason for the if-elseif expressions were so I could plug in different numbers and have the statement run it for me. That way eventually I could just have it loop through a magnitude of values for this at some point. Does that make sense what I'm saying?
These tests are independent of each other. Since you want to add points, make each test condition an if-statement with no elseif or else. Before the first test, set your initial condition for helpIndex (that is, if all conditions fail, set the desired return vfalue) .
>> so I could plug in different numbers
This a common need when modeling simulations. A common way to do this is to put all your parameters into one location, or in a separate parameters file, which would look like
ThreshAge = 50; ThreshDuration = 60; % etc.
% test conditions look like
% (age>ThreshAge) etc.
if (age>ThreshAge)
helpIndex = helpIndex + 1;
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Testing Frameworks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by