matlab - how can I take a random sample from a cell array? -


i have cell array in matlab , need take random sample, randsample() function in matlab appears not work on cell arrays. can generate random numbers using randi() fine, want unique numbers.

is there function can used randomly sample cell array, or can show me how generate unique numbers using randi()?

thanks much.

you can use randperm function generates random permutation without repeat of numbers.

for example p = randperm(n,k) gives k unique, non repeating numbers between 1 , n

randperm(10,5) gives me:

9     2     1     6     5 

randperm(10,10) gives me:

7     9     4     8     2     3     6     5     1    10 

lets have cell array

c = {'only','mad','dogs','and','englishmen','go','out','in','the','midday','sun'} 

then generate set of random phrases without repeating tokens this

output=[]; i=1:5     output = [output;sprintf('%s ',c{randperm(length(c))})]; end 

which gives me output following

out dogs in mad englishmen sun go , midday  in , midday sun englishmen out go dogs mad  out midday go in dogs , englishmen mad sun  sun out mad midday englishmen go , dogs in  midday mad sun out dogs in , go englishmen  

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -