필터 지우기
필터 지우기

How to generate a random distribution.

조회 수: 4 (최근 30일)
Dalas
Dalas 2014년 7월 11일
댓글: Image Analyst 2014년 7월 12일
Hi I'm doing a simple project for a course I'm in and I've been having trouble with some of the scripts my prof gives us. I think its because they're written for an older version of Matlab. I'm using matlab 2014a.
I think this code is supposed to generate a random distribution for x, but when I run it x ends up getting a stuck value, I know that's not supposed to happen. Can some one help?
function x=creature_state()
x=floor(2.1*rand)+1;
return
  댓글 수: 4
Dalas
Dalas 2014년 7월 12일
What I mean by stuck is that when I enter the value of x it get a singed a single value until I clear that definition of x. While the script does produce a variable value exactly like you described for some reason x ends up being a single set value.
David Young
David Young 2014년 7월 12일
Sounds like the problem is with the code that calls creature_state, not with creature_state itself. I think you need to show the relevant part of the calling code. (Best to edit the question to include it.)

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

답변 (1개)

Image Analyst
Image Analyst 2014년 7월 11일
Did you know that floor rounds down to the nearest integer? If you want unique values, get rid of the floor. It has nothing to do with the version of MATLAB either of you are using.
  댓글 수: 3
Image Analyst
Image Analyst 2014년 7월 12일
You can use randi(3, 1) to get a random number between 1 and 3. It's an integer though actually of class "double" rather than int32. It would be preferable than using that code you have.
Image Analyst
Image Analyst 2014년 7월 12일
Regarding your comment above. It does not get stuck. Look at my test3.m where I call you function:
function test3
clc; % Clear command window.
x=creature_state()
x=creature_state()
x=creature_state()
x=creature_state()
x=creature_state()
x=creature_state()
function x=creature_state()
x=floor(2.1*rand)+1;
return
Now, look at the command window:
x =
1
x =
2
x =
3
x =
3
x =
1
x =
3
Do you see that x is always the same value every time? I don't.

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

카테고리

Help CenterFile Exchange에서 Behavior and Psychophysics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by