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

5 views (last 30 days)
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))

Accepted Answer

Stephen23
Stephen23 on 22 May 2019
Edited: Stephen23 on 22 May 2019
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 Comments
Martha Spadaccino
Martha Spadaccino on 23 May 2019
>> 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'

Sign in to comment.

More Answers (1)

Martha Spadaccino
Martha Spadaccino on 23 May 2019
>> 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"

Community Treasure Hunt

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

Start Hunting!