MATLAB: Choose 2 random unique element inside cell

random unique element inside cell

how to choose 2 random unique element inside a cell?
index={[1,2,5,9,10,13,17,18,21],[4,5,7,12,13,15,18,20,21],[3,4,6,11,12,14,18,19,20],[8,16,20,22]};
example of result
p={[1,9],[7,21],[3,4],[16,8]}

Best Answer

and try
>> cellfun( @(row) datasample( row, 2, 'replace',false ), index, 'uni',false )
ans =
1×4 cell array
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
"[...] random unique element inside a cell" does that mean without replacement?
If it means literal "unique elements" then run this is a first step to remove duplicates.
>> index = cellfun( @unique, index, 'uni',false )
ix =
1×4 cell array
{1×9 double} {1×9 double} {1×9 double} {1×4 double}
Or should the propability to pick a specific value be proportional to the number of duplicates of that value?