how to remove repetition in matrix without changing length

1 view (last 30 days)
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 3x4 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
  2 Comments
Jan
Jan on 21 Dec 2015
Edited: Jan on 21 Dec 2015
EDITED, Code formatted.
What exactly is your question? I do not really understand this sentence: "I cannot however have a repetition within one column and the two columns have to stay separated and keep the length of 36 elements." Can the 2 columns do anything but staying separated?
What is the criterion for the matrix? Values from 1 to 6, the values in a row must be different, neighboring elements in the columns must be different?
Ilse Coolen
Ilse Coolen on 21 Dec 2015
I have 6 words and 6 sounds that have to be played together. Each word has to be linked to each sound once and a word can not be the same word in the following trial or a sound can not b the same sound in the following trial if that makes sense.
so in arrays I would have : words: 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 and next to that I would have the sounds: 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6.
It then has to be randomised but I keep getting repetitions

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 21 Dec 2015
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

More Answers (1)

harjeet singh
harjeet singh on 21 Dec 2015
use this code
for n = 1:6
l((n-1)*6+1:n*6,1)=n;
l((n-1)*6+1:n*6,2)=[1:6];
end
c=1:36;
B=c(randperm(length(l)));
vec_randomised=l(B',:);
  3 Comments
harjeet singh
harjeet singh on 21 Dec 2015
where you are getting the same numbers? in which row?
vec_randomised =
1 5
2 5
5 5
5 2
3 4
6 2
2 6
5 3
6 3
4 3
1 1
3 2
1 2
6 1
2 3
5 4
4 4
2 4
4 5
1 3
6 6
5 6
3 6
2 1
1 4
3 1
6 5
4 1
4 6
5 1
1 6
2 2
3 3
6 4
3 5
4 2
Ilse Coolen
Ilse Coolen on 21 Dec 2015
not in the rows but in the columns. In the first column it goes: 1 2 5 5 ... this is what I would want to avoid

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!