How can I create all possible random combinations of word pairs without having the same values (for example, house - house)?

Hi all. I have this code:
Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
idx = randi(10, 90, 2)
j = 1;
k = 1;
if ~isequal(j,k)
k = k + 1;
idx = randperm(10, 90, 2);
end
end
disp(j)
disp(k)
disp(Tile(idx))

 채택된 답변

Stephen23
Stephen23 2019년 5월 22일
편집: Stephen23 2019년 5월 22일
Pick random rows from X to get the random pairs:
>> Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
>> X = nchoosek(1:numel(Tile),2); % sorted row order.
>> Tile(X)
ans =
'house' 'core'
'house' 'word'
'house' 'ask'
'house' 'question'
'house' 'horse'
'house' 'phone'
'house' 'eyes'
'house' 'hair'
'house' 'man'
'core' 'word'
'core' 'ask'
'core' 'question'
'core' 'horse'
'core' 'phone'
'core' 'eyes'
'core' 'hair'
'core' 'man'
'word' 'ask'
'word' 'question'
'word' 'horse'
'word' 'phone'
'word' 'eyes'
'word' 'hair'
'word' 'man'
'ask' 'question'
'ask' 'horse'
'ask' 'phone'
'ask' 'eyes'
'ask' 'hair'
'ask' 'man'
'question' 'horse'
'question' 'phone'
'question' 'eyes'
'question' 'hair'
'question' 'man'
'horse' 'phone'
'horse' 'eyes'
'horse' 'hair'
'horse' 'man'
'phone' 'eyes'
'phone' 'hair'
'phone' 'man'
'eyes' 'hair'
'eyes' 'man'
'hair' 'man'
>> X = X(randperm(size(X,1)),:); % random row order.
>> Tile(X)
ans =
'core' 'word'
'horse' 'hair'
'house' 'phone'
'eyes' 'man'
'house' 'question'
'horse' 'phone'
'question' 'horse'
'horse' 'eyes'
'horse' 'man'
'question' 'hair'
'house' 'word'
'question' 'phone'
'core' 'hair'
'phone' 'hair'
'ask' 'question'
'house' 'eyes'
'word' 'eyes'
'eyes' 'hair'
'phone' 'eyes'
'core' 'phone'
'core' 'eyes'
'question' 'eyes'
'core' 'horse'
'core' 'man'
'word' 'ask'
'word' 'man'
'house' 'core'
'ask' 'eyes'
'ask' 'man'
'ask' 'hair'
'house' 'hair'
'word' 'phone'
'house' 'horse'
'house' 'ask'
'core' 'question'
'house' 'man'
'word' 'horse'
'word' 'hair'
'word' 'question'
'question' 'man'
'ask' 'phone'
'hair' 'man'
'phone' 'man'
'core' 'ask'
'ask' 'horse'

댓글 수: 5

Thank you so much, but I want a random combinations. This is in order..
"..but I want a random combinations. This is in order.."
Just randomize the rows of X to get the pairs in a random order:
>> X = X(randperm(size(X,1)),:);
>> Tile(X)
ans =
'core' 'word'
'horse' 'hair'
'house' 'phone'
'eyes' 'man'
'house' 'question'
'horse' 'phone'
'question' 'horse'
'horse' 'eyes'
'horse' 'man'
'question' 'hair'
'house' 'word'
'question' 'phone'
'core' 'hair'
'phone' 'hair'
'ask' 'question'
'house' 'eyes'
'word' 'eyes'
'eyes' 'hair'
'phone' 'eyes'
'core' 'phone'
'core' 'eyes'
'question' 'eyes'
'core' 'horse'
'core' 'man'
'word' 'ask'
'word' 'man'
'house' 'core'
'ask' 'eyes'
'ask' 'man'
'ask' 'hair'
'house' 'hair'
'word' 'phone'
'house' 'horse'
'house' 'ask'
'core' 'question'
'house' 'man'
'word' 'horse'
'word' 'hair'
'word' 'question'
'question' 'man'
'ask' 'phone'
'hair' 'man'
'phone' 'man'
'core' 'ask'
'ask' 'horse'
Done, but if I try to run the code, I have this error: "Function ''subsindex' is not defined for values of class cell"
@Martha Spadaccino: please show the exact code that you are running.
>> Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
>> X = X(randperm(size(X,1)),:);
>> Tile(X)
ans =
'core' 'word'
'horse' 'hair'
'house' 'phone'
'eyes' 'man'
'house' 'question'
'horse' 'phone'
'question' 'horse'
'horse' 'eyes'
'horse' 'man'
'question' 'hair'
'house' 'word'
'question' 'phone'
'core' 'hair'
'phone' 'hair'
'ask' 'question'
'house' 'eyes'
'word' 'eyes'
'eyes' 'hair'
'phone' 'eyes'
'core' 'phone'
'core' 'eyes'
'question' 'eyes'
'core' 'horse'
'core' 'man'
'word' 'ask'
'word' 'man'
'house' 'core'
'ask' 'eyes'
'ask' 'man'
'ask' 'hair'
'house' 'hair'
'word' 'phone'
'house' 'horse'
'house' 'ask'
'core' 'question'
'house' 'man'
'word' 'horse'
'word' 'hair'
'word' 'question'
'question' 'man'
'ask' 'phone'
'hair' 'man'
'phone' 'man'
'core' 'ask'
'ask' 'horse'

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

추가 답변 (1개)

>> Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
>> X = X(randperm(size(X,1)),:);
>> Tile(X)
>> "Function ''subsindex' is not defined for values of class cell"

카테고리

도움말 센터File Exchange에서 Licensing on Cloud Platforms에 대해 자세히 알아보기

질문:

2019년 5월 22일

답변:

2019년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by