How to choose 2 people randomly among 12 ?

조회 수: 1 (최근 30일)
Negar
Negar 2013년 10월 24일
댓글: Negar 2013년 10월 24일
Hi all,
Here is my question: I have a group of 12 peoples, Now I want to make a mini group by picking 2 of them by random. I've somehow done that, but the problem is that I want the result to be shown as a 2x1 vector, and not 2x2 matrix .
Here is my code:
Group={'ae','bk','cn','es','fh','hv','mh','ol','sd','sg','ss','ts'}; m=randi([1 12],2,1) % A 2x1 array of random numbers between 1 and 12 Minigroup=[Group{m(1)} ; Group{m(2)}]
As you see, size of the Minigroup is 2x2 . Any suggestions?
Thank you

채택된 답변

Wayne King
Wayne King 2013년 10월 24일
편집: Wayne King 2013년 10월 24일
Group={'ae','bk','cn','es','fh','hv','mh','ol','sd','sg','ss','ts'};
idx = randperm(12,2);
Group{idx}
I think you want to use randperm() instead of randi(), because randi() can give you the same index twice and presumably you want two different people.
  댓글 수: 1
Negar
Negar 2013년 10월 24일
편집: Negar 2013년 10월 24일
Thank you for the randperm, you are absolutely right ! :)
And another thing, how can I remove these two persons from my category and make a new group excluding them?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Wayne King
Wayne King 2013년 10월 24일
Group={'ae','bk','cn','es','fh','hv','mh','ol','sd','sg','ss','ts'};
idx = randperm(12,2);
ind = setdiff(1:length(Group),idx);
GroupNew = Group(ind);

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by