フィルターのクリア

I need help with Probability function.

1 回表示 (過去 30 日間)
Nadia
Nadia 2013 年 4 月 24 日
Hey guys, I need to generate a function called "produce" that will return either "peach", "panana", or "papaya" based on their probabilities of 0.20, 0.35, and 0.45 respectively. This is what I have:
function x = produce
A = 1e5;
B = rand;
C = ceil(A*B)
if (1 <= C <= 20000)
C = 'peach';
elseif ( 20001 <= C <= 55000)
C = 'panana';
elseif (55001 <= C <= 100000)
C = 'papaya';
end
x = C
For some reason, it only generates a return of "peach" no matter what the number is. It won't return panana or papaya when it's in their number range. Please help if you can. Thanks!

採用された回答

Wayne King
Wayne King 2013 年 4 月 24 日
編集済み: Wayne King 2013 年 4 月 24 日
You can't specify inequalities in a computer language like you write them down on a piece on a paper.
if (C >=1 && C <= 20000)
function x = produce
A = 1e5;
B = rand;
C = ceil(A*B)
if (C>=1 && C<= 20000)
C = 'peach';
elseif (C>= 20001 && C<= 55000)
C = 'panana';
elseif (C >= 55001)
C = 'papaya';
end
x = C
I would also suggest that you don't assign C which is originally a number a string, why not just assign x directly?
if (C>=1 && C<= 20000)
x = 'peach';
and so on?
  1 件のコメント
Nadia
Nadia 2013 年 4 月 24 日
That was it. Thanks so much!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSensors and Transducers についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by