How can use randi function for a specific array of numbers?

Hello, I am trying to generate a row of 3 random integers located within the array above. How can I tell my code to only pull numbers from the data set instead of all numbers in between?

I also tried this but it doesn't run correctly because it only recognizes scalar values. randi(dataSet(1:end),1,numberSelected)
0 Comments

Accepted Answer

Dave B on 7 Sep 2021

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard Edited: Dave B on 7 Sep 2021 How about using randi to pick indices into dataSet?

Alternatively, if you're not constrained to use randi, just use randsample instead (the randsample(population, k) syntax looks very much like your selectRandom syntax)..

selectRandom([ 74, 13, 1, 51, 6 ], 3) selectRandom([ 74, 13, 1, 51, 6 ], 4) function selectedValues = selectRandom( dataSet, numberSelected ) selectedValues = dataSet(randi(numel(dataSet), 1, numberSelected));
0 Comments

More Answers (0)

See Also

Categories

Find more on Random Number Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.