# how to remove repetition in matrix without changing length

1 回表示 (過去 30 日間)
Ilse Coolen 2015 年 12 月 21 日
コメント済み: Ilse Coolen 2015 年 12 月 21 日
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 件のコメントなしを表示なしを非表示
Jan 2015 年 12 月 21 日

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 2015 年 12 月 21 日
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

サインインしてコメントする。

### 採用された回答

Guillaume 2015 年 12 月 21 日
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
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Ilse Coolen 2015 年 12 月 21 日
thank you!!!

サインインしてコメントする。

### その他の回答 (1 件)

harjeet singh 2015 年 12 月 21 日
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 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
harjeet singh 2015 年 12 月 21 日
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 2015 年 12 月 21 日
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

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by