フィルターのクリア

Auto fill zero matrix without row-repetitions

3 ビュー (過去 30 日間)
JB
JB 2017 年 6 月 4 日
コメント済み: Andrei Bobrov 2017 年 6 月 5 日
Please help. I'm new to matlab scripting and need a bit of help. I have a series of numbers: test = [1 1 1 2 2 2 3 3 3 4 4 4 5 5 5] which I wants to randomely fill into a 5x3 matrix without having the same number in the same row. How can I do this??? Potentially I could randomize the test vector and fill it into the 5x3 matrix but I dont know how to do this without getting the same number in the same row. PLEASE help...
  1 件のコメント
JB
JB 2017 年 6 月 5 日
MISTAKE, I want the final matrix to be a 3x5 matrix and not a 5x3 matrix...

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

採用された回答

JB
JB 2017 年 6 月 5 日
Thanks a lot for your suggestions. However, I made a minor mistake. I want the final matrix to be a 3x5 instead of a 5x3 matrix. Sorry guys, but I still need a bit of help to establish the correct code.
  3 件のコメント
JB
JB 2017 年 6 月 5 日
Awesome, thanks KSSV. I have one final question for you which I hope you can help me with. Say test = [1 1 1 2 2 2 3 3 3] and I want to fill it into a 3x5 matix without row repetionen, and leave excess cells blank (or =) how is this done?
KSSV
KSSV 2017 年 6 月 5 日
test = [1 1 1 2 2 2 3 3 3] ;
A = NaN(3,5) ;
test_unique = unique(test) ;
for i = 1:3
idx = randperm(3) ;
A(i,randperm(5,3)) = test_unique(idx) ;
end

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

その他の回答 (2 件)

KSSV
KSSV 2017 年 6 月 5 日
You can take the unique matrix of test and pick any three elements out of it and fill in the required 5X3 matrix.
test = [1 1 1 2 2 2 3 3 3 4 4 4 5 5 5] ;
test_unique = unique(test) ;
A = zeros(5,3) ;
for i = 1:size(A,1)
A(i,:) = randsample(test_unique,3) ;
end
randsample needs a statistics toolbox, if you doesn't have it, you may use randperm as shown below.
test = [1 1 1 2 2 2 3 3 3 4 4 4 5 5 5] ;
test_unique = unique(test) ;
A = zeros(5,3) ;
for i = 1:size(A,1)
A(i,:) = test_unique(randperm(length(test_unique),3)) ;
end
  3 件のコメント
KSSV
KSSV 2017 年 6 月 5 日
You mean to say element might repeat in a row?
Walter Roberson
Walter Roberson 2017 年 6 月 5 日
Elements cannot repeat in any row, but all elements of T must be placed in the 5 x 3 matrix.
Your code prevents elements from repeating in any one row, but the number of copies of any given member of T is not the same as the original. For example your code could randomly create
[1 2 3
1 2 3
1 2 3
1 2 3
1 2 3]
with no 4 or 5 anywhere.

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


Andrei Bobrov
Andrei Bobrov 2017 年 6 月 5 日
編集済み: Andrei Bobrov 2017 年 6 月 5 日
m = 5;
n = 3;
A = reshape(test,n,m)';
out = A(bsxfun(@plus,hankel(1:m,[m,1:n-1]),m*(0:n-1)));
out = out(randperm(m),:);
out = out(:,randperm(n));
ADD
m = 5;
n = 3;
test = repelem(1:3,3);
A = nan(n,m);
A(1:numel(test)) = test;
[~,ii] = sort(rand(n,m),2);
out = A(bsxfun(@plus,n*(ii-1),(1:n)'));
  4 件のコメント
JB
JB 2017 年 6 月 5 日
Awesome, thanks Andrei. I have one final question for you which I hope you can help me with. Say test = [1 1 1 2 2 2 3 3 3] and I want to fill it into a 3x5 matix without row repetionen, and leave excess cells blank (or =) how is this done?
Andrei Bobrov
Andrei Bobrov 2017 年 6 月 5 日
I'm corrected ADD - part in my answer.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by