Selecting random x number from "N" input numbers.

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.)

回答 (1 件)

Rik
Rik 2022 年 12 月 19 日

0 投票

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)
ABBABAAAAA

カテゴリ

ヘルプ センター および File ExchangeParticle & Nuclear Physics についてさらに検索

質問済み:

2022 年 12 月 19 日

回答済み:

Rik
2022 年 12 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by