calculating percentage change function

19 ビュー (過去 30 日間)
Sarah K
Sarah K 2019 年 10 月 22 日
コメント済み: dpb 2019 年 10 月 22 日
Hi,
I am high school china. want to build a function for two input variables (two judges scores) and calculate the percentage difference between them.
function [outcome] = judges_scores(input1, input2)
pct_difference = 100*(input1-input2)./input2);
if (pc_difference = 0)
disp('pass');
else (pc_difference < 0) && (pc_difference > 0)
disp('fail');
end
I would like some assistance, especially how to calculate percentage difference for two inputs in a function. Thank you.
  1 件のコメント
dpb
dpb 2019 年 10 月 22 日
Your function calculates the percentage difference between the two judges' scores presuming the second is the reference. What you'll get as a numeric value will depend on which is the reference...if the reference is higher than the other, you'll get a value <0 of somewhat smaller absolute magnitude than vice versa:
>> J1=95;J2=92;
>> (J1/J2-1)*100
ans =
3.2609
>> (J2/J1-1)*100
ans =
-3.1579
>>
How to treat this is up to your problem definition which isn't given so we can't answer that part.
NOTA BENE:
You defined the percent difference as pct_difference, however you missed the t on pct in the |if...else...end clause so it's undefined.
You also called your function return value outcome but it is never defined in the function so don't have any return value.

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2019 年 10 月 22 日
You should use = for an assignment and == for a comparison. Another point is that you don't need a conditional for an else, only for an elseif.
Note that a comparison with 0 might lead to unexpected results in edge cases. Comparing the abs to eps should be more reliable (and would provide an easy way to modify your tolerance.
  1 件のコメント
Star Strider
Star Strider 2019 年 10 月 22 日
Also note that ‘outcome’ is not assigned.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by