Creating groups of 2 people from a cell array of names

조회 수: 4 (최근 30일)
Areesh Adil
Areesh Adil 2020년 3월 11일
댓글: Bhaskar R 2020년 3월 11일
If I have a n x 1 cell array of names and want to create random 2-person groups out of the names without repeats so that I have a (n/2) x 2 cell array, how would I go about doing so? Just an idea on how to start or a hint would also be helpful.

채택된 답변

the cyclist
the cyclist 2020년 3월 11일
If C is your cell array, then
reshape(C(randperm(n)),n/2,2)

추가 답변 (1개)

Bhaskar R
Bhaskar R 2020년 3월 11일
n = 26;
x = cellstr(char([65:65+n-1]')); % your cell data assumed
rand_nums = reshape(randsample(n,n), n/2, 2); % random numbers
rand_cell = x(rand_nums);
  댓글 수: 2
Areesh Adil
Areesh Adil 2020년 3월 11일
Thank you. Could you explain what you did? I would like to learn what's happening rather than copying your code. But this is still super helpful nonetheless so thanks.
Bhaskar R
Bhaskar R 2020년 3월 11일
i created a dummy cell data x which contains character data(A to Z characters each character is a cell). Then created random samples(unique random values) and reshaped as 2 pairs as you require there are random locations to extract data from x

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by