MATLAB: How to remove repetition in matrix without changing length

randomrepetition

Hi,
I'm trying to create an experiment with matlab and I am using 2 columns in a matrix with number I have randomized according to an index.
I cannot however have a repetition within one column and the two columns have to stay separated and keep the length of 36 elements.
so this is how I created my matrix:
for n = 1:6
l((n-1)*6+1:n*6,1)=n
l((n-1)*6+1:n*6,2)=[1:6]
end
vec_index=randperm(36)
vec_randomised=l(vec_index',:)
which gives me this:
5 3
4 2
4 1
4 6
5 5
6 3
1 5
1 2
1 6
6 5
3 2
6 1
5 4
1 4
2 3
3 5
5 2
2 2
6 2
4 3
2 6
3 6
5 1
2 5
6 4
2 1
6 6
3 4
3 3
4 4
5 6
1 1
2 4
3 1
1 3
4 5
as you can see there are repetitions for example the 3×4 in the first column.
I made a for-loop to detect the repetition but this does not help me to remove them and keeping the same numbers/amount of numbers in the column.
I also tried shuffle but this gives me different numbers.
I hope I need this as I want to couple this array to words and sounds (which I'm also failing at)
Hopefully someone can help! thx

Best Answer

with the size of your matrix, a brute force approach of generating a new permutation until it matches your requirement seems feasible. On the few test I did, it only took a maximum of 1 second:
%a better way to generate the original matrix:
numwords = 6;
numsounds = 6; %doesn't have to be the same number as numwords
wordperm = repmat(1:numwords, numsounds, 1);
wordsound = [wordperm(:), repmat([1:numsounds]', numwords, 1)];
%brute force search of random permutation with no two consecutive number in any column
tic
while any(any(diff(wordsound) == 0))
wordsound = wordsound(randperm(numwords * numsounds), :);
end
toc
wordsound