Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab, mathematical algorithm

  1. Oct 29, 2006 #1
    Hey there,

    I was just wondering if anyone in here could help me out with a short algorithm I have to write for my class.

    Lets say the function fix(10*rand(1,1)) gives u a random number out of (0,1,2,3,4,5,6,7,8,9)

    now, you need to use the number from the generator to pick out of 3 objects (A,B,C)

    i.e pick = fix(10*rand(1,1))

    What I'm thinking is to assign A = [1,2,3] B = [4,5,6] C = [7,8,9]

    but where I get stuck is what if the random generator gives me a 0?

    Could I just write another function saying that if pick = 0 then to run the random function again? But I feel that this would not make the program truly dependant on rand(1,1) which we are instructed to use. But then there is no way to assing 3 objects to 10 numbers??

    Any suggestions are much appreciated. Thank You
     
  2. jcsd
  3. Oct 30, 2006 #2
    Add 1 to 9*rand(1,1)? My only concern is matlab is a nightmare to use (syntactically) so I'm not sure if the answer is that simple.
     
  4. Oct 31, 2006 #3
    Try using
    pick = ceil(3*rand)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?