Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Sort random samples from vector into matrix such that there are no repeated elements in each column

1 view (last 30 days)
Hi everyone. I have a vector that has the numbers from 1 to 256 twice, something like:
y = [1:256 1:256];
I want to randomly sort all the elements of this vector into a 64 by 8 matrix, in such a way that there are no repeated elements in a single column. My first approach was to do a loop, taking data samples from the vector y and keep the first case where there are no repeated elements, something like:
for i=1:8
[samps,idx]=datasample(y,64,'Replace',false);
while(length(samps) ~= length(unique(samps)))
[samps,idx]=datasample(y,64,'Replace',false);
end
matrix(:,i)=samps;
y(idx)=[];
end
This works sometimes, but sometimes it doesn't. The problem is when we reach the last column, sometimes the remaining 64 elements from the y vector have some repeated values (there are more solutions to the problem without meeting the criteria of non repeated elements by columns than the solutions that do meet it).
The complete version of this problem is when the vector y can have any number of elements, but always a mutiple of 8. For example, if y has 512+32 elements:
full_vec = 2;
y = repmat(1:256,[1 full_vec]);
y = [y randperm(256,32)];
The idea is that if the vector y has N*256 elements, each number from 1 to 256 will be repeated exactly N times. But the vector y can also have N*256+n*8 elements, so the numbers from 1 to 256 will be repeated N times, while we will also have n*8 extra random numbers from 1 to 256.
The problem is always with the sorting though: if the vector y has a total of K*8 elements (K from 32 to 256), how to sort, at random (it has to be random), this elements into a K by 8 matrix such that there are no repeated elements in each column?
Thanks in advance for any help I can get.

Answers (0)

This question is closed.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!