how to take random words from a sting matrix
10 views (last 30 days)
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
0 Comments
Answers (2)
Voss
on 18 Jan 2024
Words = ["a","1","x","9","string","matrix","full","of","words"]
idx = randperm(numel(Words))
inOtherWords = Words(idx(1:5))
5 Comments
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"
Image Analyst
on 18 Jan 2024
"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)
See Also
Categories
Find more on Characters and Strings 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!