Question

Suppose a matrix is given say d.

d=[1.32 4.354 6.78 4.56; 
   4.65 3.23  2.34 8.9; 
   2.32 7.65  4.98 2.78]

I require to pick values such that no two values chosen are same. Say I want to pick 5 values in an increment of 1 using Matlab.

for ii=1:5
  % pick value one by one such that no two values chosen are same
end

loop should function like this:

  ii=1, d'=4.354
  ii=2, d'=4.354 2.32

It shouldn't be :

   ii=2, d'=4.354 4.354 
Was it helpful?

Solution

Just use randperm to create a linear index of 5 random non-repeating numbers:

d(randperm(numel(d),5))

OTHER TIPS

If you want to pick unique values for d, simply use unique to remove replicates before picking:

 ud = unique( d(:) ); % note the (:) - we want element-wise unique and not row-unique

 ud(1:5) % picks first fiver unique elements
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top