what does this code represents? and why rand() has no value between parentheses?

1 回表示 (過去 30 日間)
william Smith
william Smith 2019 年 3 月 22 日
コメント済み: william Smith 2019 年 3 月 22 日
function [outcome]=customer
x=rand() ;
if x <= 0.2
outcome = 1 ;
else
outcome = 0;
end

採用された回答

Stephen23
Stephen23 2019 年 3 月 22 日
編集済み: Stephen23 2019 年 3 月 22 日
It defines a function which returns either 1 or 0, where 1 has ~=20% probability. Simpler code:
customer = @()+(rand(1)<=0.2);
"why rand() has no value between parentheses?"
Did you read the rand documentation? It explains the different rand syntaxes.

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 3 月 22 日
編集済み: James Tursa 2019 年 3 月 22 日
rand() is the same as rand without the parentheses ... they both simply call the rand function with no input arguments. The behavior of rand is that when called with no input arguments, it returns a scalar.
This function code simply gets a random number between 0 and 1. If it is less than or equal to 0.2, the outcome is assigned a value of 1. Otherwise the outcome is assigned a value of 0. The value of outcome is returned by the function.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by