how to take random words from a sting matrix
Show older comments
i have a 1x9 matrix full of words, i want to randomly take 5 of them and put them into another matrix of 1x5 how would i go about this?
id also like to do something similar where i take a random number from a 1x20 array and asign it to a variable
Answers (2)
Words = ["a","1","x","9","string","matrix","full","of","words"]
idx = randperm(numel(Words))
inOtherWords = Words(idx(1:5))
5 Comments
That's one way to select elements without replacement (think dealing cards from a deck.) For selection with replacement (rolling a bunch of dice) use randi.
Words = ["a","1","x","9","string","matrix","full","of","words"]
idx = randi(numel(Words), 5, 5)
inOtherWords = Words(idx)
finley
on 19 Jan 2024
finley
on 19 Jan 2024
@finley: What seems to be the problem?
outputlootpool = ["ruby","coin";"cash","gold"]
any( outputlootpool(1,:) == "coin" )
Walter Roberson
on 19 Jan 2024
ismember("coin", outputlootpool(1,:))
will return a single true / false value indicating whether "coin" is present
ismember(outputlootpool(1,:), "coin")
will return a vector the size of outputlootpool(1,:) indicating whether each one is "coin"
"id also like to do something similar where i take a random number from a 1x20 array and asign it to a variable"
Try this:
yourVector = rand(1, 20) % Create sample data.
randomIndex = randi(numel(yourVector), 1)
yourVariable = yourVector(randomIndex)
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!