Randomized array with limits

I am having a huge problem in generating a randomized array with 3 lines and for collumns that has to be of this type 2 1 3 1
3 2 1 2
1 3 2 3;
basicly the limits are in each column their has to exist at least this a number 1, 2 and 3, because it has 4 columns one of the numbers will always repeat in each line obviously, this is what i have done so far but can't seem to do better than this.
mp=[];
mp1=randperm(3)';
mp2=randperm(3)';
mp3=randperm(3)';
mp=[mp mp1 mp2 mp3]

2 件のコメント

Walter Roberson
Walter Roberson 2020 年 12 月 12 日
If you used the same randperm for the initial mp then you would get 4 columns.
ricardo Alcobia
ricardo Alcobia 2020 年 12 月 12 日
Yeah i know but it would not obey the rules, thank you anyways

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

回答 (2 件)

Rik
Rik 2020 年 12 月 12 日

0 投票

A=1:3;A(4)=randi(3);
mp=A(randperm(end))

2 件のコメント

ricardo Alcobia
ricardo Alcobia 2020 年 12 月 12 日
Ybut that only generates an array with 1 line, it has to have 3 lines, and even if i did that 3 times i had no guarantee that in each column there would be at least a 1, a 2 and a 3.
Thank for your help anyways!
Rik
Rik 2020 年 12 月 12 日
Do you also have a rule for each row?

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

Image Analyst
Image Analyst 2020 年 12 月 12 日

0 投票

Try this:
% Initialize.
rows = 3;
columns = 4;
% Each row of A starts out like [1, 2, 3, rowNumber] to follow the poster's example/directions.
% So first row will have 2 1s, second row will have 2 2s, and third row will have 2 3s.
A= [repmat(1:columns-1, [rows, 1]), (1:rows)']
mp = A;
% Go down each row, scrambling the columns
for row = 1 : size(A, 1)
order = randperm(size(A, 2));
mp(row, :) = A(row, order);
end
mp
You'll see
A =
1 2 3 1
1 2 3 2
1 2 3 3
mp =
1 2 3 1
2 3 1 2
1 3 3 2
as required.

3 件のコメント

ricardo Alcobia
ricardo Alcobia 2020 年 12 月 12 日
That does not obey to the fact that every column has to have at least one of each number, thanks for your help!
Btw I already found a way to do this!
a=0;
while a<8
MP1=randperm(3);
MP2=randperm(3);
MP3=randperm(3);
MP4=randperm(3);
MP=[MP1;MP2;MP3;MP4]';
mp11=1;
mp22=1;
mp33=1;
for j=1:4
mp1=MP(1,j);
mp11=mp11*mp1;
mp2=MP(2,j);
mp22=mp22*mp2;
mp3=MP(3,j);
mp33=mp33*mp3;
end
b=prod(MP,'all');
if mp11==18 | mp11==12 | mp11==6
ab=2;
else
ab=0;
end
if mp22==18 | mp22==12 | mp22==6
aa=2;
else
aa=0;
end
if mp33==18 | mp33==12 | mp33==6
aaa=2;
else
aaa=0;
end
if b==1296
aaaa=2;
else
aaaa=0;
end
a=aa+aaa+aaaa+ab;
end
MP
Image Analyst
Image Analyst 2020 年 12 月 12 日
編集済み: Image Analyst 2020 年 12 月 12 日
I guess I misinterpreted what you said and your example. Try this code (much shorter and simpler than yours):
% Initialize.
rows = 3;
columns = 4;
% Each column of A starts out like [1; 2; 3]. Each row will be the row number.
% then we'll scramble the order of the rows, column-by-column.
mp = [repmat((1:rows)', [1, columns])]
% Go across each column, scrambling the rows in each column.
for col = 1 : columns
order = randperm(rows);
mp(:, col) = mp(order, col);
end
mp
You'll get, for example:
mp =
1 1 2 2
3 3 1 3
2 2 3 1
Looks like you maybe want a "Latin Rectangle". Have you ever heard of that?
ricardo Alcobia
ricardo Alcobia 2020 年 12 月 12 日
yeah that is it thank you very much

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2020 年 12 月 12 日

編集済み:

2020 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by