How Can I Assign 3 Objects to 10 Randomly Generated Numbers in Matlab?

Click For Summary
SUMMARY

The discussion focuses on assigning three objects (A, B, C) to ten randomly generated numbers using MATLAB. The user initially suggests using the function fix(10*rand(1,1)) to generate numbers from 0 to 9 but encounters an issue with the number 0, which does not correspond to any object. A proposed solution is to use pick = ceil(3*rand), which effectively maps the random output to the three objects without generating a 0. This method ensures that each object is assigned based on the random number generated.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with random number generation in programming
  • Basic knowledge of arrays and indexing in MATLAB
  • Concept of mapping values to specific categories
NEXT STEPS
  • Research MATLAB's rand and ceil functions for random number generation
  • Explore array manipulation techniques in MATLAB
  • Learn about conditional statements in MATLAB for handling exceptions
  • Investigate advanced randomization techniques in MATLAB for more complex scenarios
USEFUL FOR

Students learning MATLAB programming, software developers working with randomization algorithms, and anyone interested in object assignment based on random number generation.

sapiental
Messages
110
Reaction score
0
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 dependent 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
 
Physics news on Phys.org
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.
 
Try using
pick = ceil(3*rand)
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
2
Views
3K