How to round numbers using the decimals as a probability?

조회 수: 4 (최근 30일)
Aron
Aron 2023년 4월 13일
댓글: Walter Roberson 2023년 4월 13일
Hi, I am trying to create code where the decimals are used as probability of how the value is rounded. For example, I have array of the following values: [ 1.04 ; 0.96 ; 0.22 ; 1.56 ] and for the first element there is a 4% chance the value gets rounded up to 2 and a 96% chance it gets rounded to 1. For the second element there is a 96% probability that it gets rounded up to 1 and a 4% it gets rounded to 0.
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 4월 13일
What is the rule? Round down 96% of the time for values with even row numbers, and round down 96% of the time for values with odd row numbers? If so then should that be modified if there are multiple columns ?

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2023년 4월 13일
A=10*rand(1,10)
A = 1×10
3.8438 8.9825 9.7798 3.3108 6.2760 5.4551 4.8986 1.2530 4.2234 9.1930
B=floor(A)+((A-floor(A))>rand(1,10))
B = 1×10
3 9 9 3 7 6 5 1 5 9
  댓글 수: 3
Aron
Aron 2023년 4월 13일
Thanks, I changed it to
A=10*rand(1,10)
B=floor(A)+((A-floor(A))>rand(1,1))
Because otherwise B is the of the size 3x10.
Walter Roberson
Walter Roberson 2023년 4월 13일
No, each entry in A should be rounded individually.
B=floor(A)+((A-floor(A))>rand(size(A)))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by