Selecting random x number from "N" input numbers.
Show older comments
QUESTION: How to select random x% percentage of number from a given input of "n" sets of number?
(BACKGROUND FOR QUESTION) I have a mechanical simulation in which I have to give 2 different material properties( mixture of 30% A material randomly distributed in 70% B material). So I if my model have for (eg: 1000 elements I want to select random 30% of element(300) and make one set to give material property A, and remaining (700) as material B.)
Answers (1)
You can use randperm (remember to round the percentage to an integer number of positions):
percentage = 30;
total = 10;
output = repmat('A',1,total);
output(randperm(end,round(end*percentage/100))) = 'B';
disp(output)
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!