PDA

View Full Version : matlab, mathematical algorithm


sapiental
Oct29-06, 08:04 PM
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

vsage
Oct30-06, 01:23 PM
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.

cbmoler
Oct31-06, 03:59 AM
Try using
pick = ceil(3*rand)