How to generate unique random numbers in Matlab? - matlab

I need to generate m unique random numbers in range 1 to n. Currently what I have implemented is:
round(rand(1,m)*(n-1)+1)
However, some numbers are repeated in the array. How can I get only unique numbers?

You can use randperm.
From the description:
p = randperm(n,k) returns a row vector containing k unique integers
selected randomly from 1 to n inclusive.
Thus, randperm(6,3)
might be the vector
[4 2 5]
Update
The two argument version of randperm only appeared in R2011b, so if you are using an earlier version of MATLAB then you will see that error. In this case, use:
A = randperm(n);
A = A(1:m);

As pointed out above, in Matlab versions older than R2011b randperm only accepts one input argument. In that case the easiest approach, assuming you have the Statistics Toolbx, is to use randsample:
randsample(n,m)

The randperm approach described by #Stewie appears to be the way to go in most cases. However if you can only use Matlab with 1 input argument and n is really large, it may not be feasible to use randperm on all numbers and select the first few. In this case here is what you can do:
Generate an integer between 1 and n
Generate an integer between 1 and n-1, this is the choice out of the available integers.
Repeat until you have m numbers
This can be done with randi and could even be vectorized by just drawing a lot of random numbers at each step until the unique amount is correct.

Use Shuffle, from the MATLAB File Exchange.
Index = Shuffle(n, 'index', m);

This can be done by sorting a random vector of floats:
[i,i]=sort(rand(1,range));
output=i(1:m);

Related

How to generate all possible nxm arrays, if each element is binary (can only take on 0 or 1). Preferably in matlab

I am essentially trying to generate all possible nxm matrices. I have seen some codes in R and Python that kind of does this with a single function, but I cant find anything similar for matlab :(
here's a way to do that in matlab:
n=4;
m=3;
for c=0:2^(n*m)-1
A(:,c+1)=str2double(split(dec2bin(c,n*m),'',1));
end
A(1,:)=[]; A(end,:)=[];
A=reshape(A,n,m,[]);
the logic, it doesnt matter if it is an nxm array or a 1D vector of length nxm, we can later reshape the vector to an nxm array (last line). Then, the # of possible permutation for a binary string of Length L is just all the decimal # from 0 to 2^L-1 transformed to binary string of length nxm. This is what the dec2bin(...) function does. Then we get a long string (for example '010001010111') that needs to be split to individual elements('0','1',...) , so we can later convert this strings to numbers using str2double. The split(...) function does that but generates NaN at the edges, so we get rid of them in the row before the last one...
so for this example I chose n=4, m=3, which generates just 2^(nxm)=4096 possible array...
you can see them if if you try A(:,:,j) with j being a # from 1 to 4096

Retrieve a specific permutation without storing all possible permutations in Matlab

I am working on 2D rectangular packing. In order to minimize the length of the infinite sheet (Width is constant) by changing the order in which parts are placed. For example, we could place 11 parts in 11! ways.
I could label those parts and save all possible permutations using perms function and run it one by one, but I need a large amount of memory even for 11 parts. I'd like to be able to do it for around 1000 parts.
Luckily, I don't need every possible sequence. I would like to index each permutation to a number. Test a random sequence and then use GA to converge the results to find the optimal sequence.
Therefore, I need a function which gives a specific permutation value when run for any number of times unlike randperm function.
For example, function(5,6) should always return say [1 4 3 2 5 6] for 6 parts. I don't need the sequences in a specific order, but the function should give the same sequence for same index. and also for some other index, the sequence should not be same as this one.
So far, I have used randperm function to generate random sequence for around 2000 iterations and finding a best sequence out of it by comparing length, but this works only for few number of parts. Also using randperm may result in repeated sequence instead of unique sequence.
Here's a picture of what I have done.
I can't save the outputs of randperm because I won't have a searchable function space. I don't want to find the length of the sheet for all sequences. I only need do it for certain sequence identified by certain index determined by genetic algorithm. If I use randperm, I won't have the sequence for all indexes (even though I only need some of them).
For example, take some function, 'y = f(x)', in the range [0,10] say. For each value of x, I get a y. Here y is my sheet length. x is the index of permutation. For any x, I find its sequence (the specific permutation) and then its corresponding sheet length. Based on the results of some random values of x, GA will generate me a new list of x to find a more optimal y.
I need a function that duplicates perms, (I guess perms are following the same order of permutations each time it is run because perms(1:4) will yield same results when run any number of times) without actually storing the values.
Is there a way to write the function? If not, then how do i solve my problem?
Edit (how i approached the problem):
In Genetic Algorithm, you need to crossover parents(permutations), But if you crossover permutations, you will get the numbers repeated. for eg:- crossing over 1 2 3 4 with 3 2 1 4 may result something like 3 2 3 4. Therefore, to avoid repetition, i thought of indexing each parent to a number and then convert the number to binary form and then crossover the binary indices to get a new binary number then convert it back to decimal and find its specific permutation. But then later on, i discovered i could just use ordered crossover of the permutations itself instead of crossing over their indices.
More details on Ordered Crossover could be found here
Below are two functions that together will generate permutations in lexographical order and return the nth permutation
For example, I can call
nth_permutation(5, [1 2 3 4])
And the output will be [1 4 2 3]
Intuitively, how long this method takes is linear in n. The size of the set doesn't matter. I benchmarked nth_permutations(n, 1:1000) averaged over 100 iterations and got the following graph
So timewise it seems okay.
function [permutation] = nth_permutation(n, set)
%%NTH_PERMUTATION Generates n permutations of set in lexographical order and
%%outputs the last one
%% set is a 1 by m matrix
set = sort(set);
permutation = set; %First permutation
for ii=2:n
permutation = next_permute(permutation);
end
end
function [p] = next_permute(p)
%Following algorithm from https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
%Find the largest index k such that p[k] < p[k+1]
larger = p(1:end-1) < p(2:end);
k = max(find(larger));
%If no such index exists, the permutation is the last permutation.
if isempty(k)
display('Last permutation reached');
return
end
%Find the largest index l greater than k such that p[k] < p[l].
larger = [false(1, k) p(k+1:end) > p(k)];
l = max(find(larger));
%Swap the value of p[k] with that of p[l].
p([k, l]) = p([l, k]);
%Reverse the sequence from p[k + 1] up to and including the final element p[n].
p(k+1:end) = p(end:-1:k+1);
end

Randomly select subset of all possible combinations in Matlab?

I need to select random combinations of k elements from a set of n elements, where n can be fairly large. Given the size of the set, it is not feasible to simply use combnk or nchoosek to generate all possible combinations, and select randomly from those.
Is there an easy way to generate a unique random subset of M of those combinations?
When n is small, the following works:
M = 20; %want to pick M random combinations
n = 10; %number of elements
k = 5; %number of elements in each combination
allCombos = nchoosek([1:n], k); %for large n this is not feasible
numCombos = nchoosek(n,k);
permutationsToUse = randperm(numCombos, M);
randomCombos = allCombos(permutationsToUse, :);
When n is large, this is no longer feasible.
Related Posts
Retrieve a specific permutation without storing all possible permutations in Matlab
How to randomly pick a number of combinations from all the combinations efficiently?
Select a subset of combinations
You can try using randi and generate random combinations of 7 integers from 1 to Nelements and then check that you only have unique combinations:
Nelements=100;
M=10;
combsubset=randi(Nelements,[M 7]);
combsubset=unique(combsubset,'rows');
If you want to get exactly M combinations you can use a loop:
Nelements=100;
M=10;
combsubset=[];
while(size(combsubset,1)<M)
combsubset=[combsubset;randi(Nelements,[M 7])];
combsubset=unique(combsubset,'rows');
end
combsusbet=combsubset(1:M,:);
If you want to reuse this to get other combinations you can pretty much use the same code:
Nelements=100;
Mtotal=20
M=10;
while(size(combsubset,1)<Mtotal)
combsubset=[combsubset;randi(Nelements,[M 7])];
combsubset=unique(combsubset,'rows');
end
combsusbet=combsubset(1:Mtotal,:);
EDIT: Another method for your needs would be to order the combinations to be able to get only a given subset. One method to order them can be explained with the following example: if you have three indices i,j,k ranging from 0 to N-1 you can use a unique index n=i*N*N+j*N+k to go over all the possibilities. Then if you want to get the nth vector:
k=mod(n,N);
j=mod((n-k)/N,N);
i=mod((((n-k)/N)-j)/N,N);
I do not know if you will find this more elegant but with the help of a little function that uses recursion you could easily get a fixed subset of your combinations.

How can I generate a map key for this vector in MATLAB?

I have a function that is looking at a number of elements. Each element is of the form of an 8x1 column vector. Each entry in the vector is an integer less than 1000. Every time I see such a vector, I'd like to add it to a list of "already seen" vectors, after checking to see that the vector is not already on this list. The function will examine on the order of ~100,000 such vectors.
Originally I tried using ismember(v', M, 'rows'), but found this to be very slow. Next I tried:
found = containers.Map('KeyType', 'double', 'ValueType', 'any');
Then each time I examine a new vector v, compute:
key = dot(v, [1000000000000000000000 1000000000000000000 1000000000000000 ...
1000000000000 1000000000 1000000 1000 1]);
Then check isKey(found, key). If the key is not in the container, then found(key) = 1.
This seems like a pretty lousy solution, even though it does run considerably faster than ismember. Any help/suggestions would be greatly appreciated.
EDIT: Perhaps it would be better to use mat2str to generate the key, rather than this silly dot product?
The easiest way to generate a key/hash in your case would be to just convert the vector of integer values to a character array using char. Since your integer values never go above 1000, and char can accept numeric values from 0 to 65535 (corresponding to Unicode characters), this will give you a unique 8-character key for every unique 8-by-1 vector. Here's an example:
found = containers.Map('KeyType', 'char', 'ValueType', 'any');
v = randi(1000, [8 1]); % Sample vector
key = char(v);
if ~isKey(found, key)
found(key) = 1;
end
your idea is good. but you need to find a better hash function. use some standard hash function.
There is an implementation of 'sha' algorithms you's like to see:
http://www.se.mathworks.com/matlabcentral/fileexchange/31795-sha-algorithms-160224256384-512
If you find the sha algorithm slow then you can probably resort to some tricks. One that i can think of now is following:
take a seed number > 1000 e.g. 1024
divide each number in the vector by the seed and store the remainder in a string.
concatenate all the remainders which will serve as your 'code' for your vector element. which can be used for comparison when a you see a new element.
this should probably work but you'll have to check.
Not really into hashing, but still believe to have found the simplest way to solve your problem.
This runs about 10x faster than ismember.
any(v(1)==M(1)&v(2)==M(2)&v(3)==M(3)&v(4)==M(4)&v(5)==M(5)&v(6)==M(6)&v(7)==M(7)&v(8)==M(8));

Compact MATLAB matrix indexing notation

I've got an n-by-k sized matrix, containing k numbers per row. I want to use these k numbers as indexes into a k-dimensional matrix. Is there any compact way of doing so in MATLAB or must I use a for loop?
This is what I want to do (in MATLAB pseudo code), but in a more MATLAB-ish way:
for row=1:1:n
finalTable(row) = kDimensionalMatrix(indexmatrix(row, 1),...
indexmatrix(row, 2),...,indexmatrix(row, k))
end
If you want to avoid having to use a for loop, this is probably the cleanest way to do it:
indexCell = num2cell(indexmatrix, 1);
linearIndexMatrix = sub2ind(size(kDimensionalMatrix), indexCell{:});
finalTable = kDimensionalMatrix(linearIndexMatrix);
The first line puts each column of indexmatrix into separate cells of a cell array using num2cell. This allows us to pass all k columns as a comma-separated list into sub2ind, a function that converts subscripted indices (row, column, etc.) into linear indices (each matrix element is numbered from 1 to N, N being the total number of elements in the matrix). The last line uses these linear indices to replace your for loop. A good discussion about matrix indexing (subscript, linear, and logical) can be found here.
Some more food for thought...
The tendency to shy away from for loops in favor of vectorized solutions is something many MATLAB users (myself included) have become accustomed to. However, newer versions of MATLAB handle looping much more efficiently. As discussed in this answer to another SO question, using for loops can sometimes result in faster-running code than you would get with a vectorized solution.
I'm certainly NOT saying you shouldn't try to vectorize your code anymore, only that every problem is unique. Vectorizing will often be more efficient, but not always. For your problem, the execution speed of for loops versus vectorized code will probably depend on how big the values n and k are.
To treat the elements of the vector indexmatrix(row, :) as separate subscripts, you need the elements as a cell array. So, you could do something like this
subsCell = num2cell( indexmatrix( row, : ) );
finalTable( row ) = kDimensionalMatrix( subsCell{:} );
To expand subsCell as a comma-separated-list, unfortunately you do need the two separate lines. However, this code is independent of k.
Convert your sub-indices into linear indices in a hacky way
ksz = size(kDimensionalMatrix);
cksz = cumprod([ 1 ksz(1:end-1)] );
lidx = ( indexmatrix - 1 ) * cksz' + 1; #'
% lindx is now (n)x1 linear indices into kDimensionalMatrix, one index per row of indexmatrix
% access all n values:
selectedValues = kDimensionalMatrix( lindx );
Cheers!