MATLAB Answers

0

Random and unique number generation with constraints.

Asad Abbas さんによって質問されました 2016 年 9 月 6 日
最新アクティビティ Asad Abbas さんによって コメントされました 2016 年 9 月 7 日
Please help me. Here is given my given code where random number generation of 0 and 1 and find the unique solutions with given matrix 119*9.
pop_size = 119;
x = randi([0,1],pop_size,9);
chk = true;
while chk
[~,idx] = unique(x,'rows');
idy = setdiff(1:pop_size,idx);
x(idy,:) = randi([0,1],numel(idy),9);
chk = numel(idy)>0;
end
I want apply some constraints.
1. alternative values in c1 (first column) and c2 (second column) such as
c1 c2 c3 c4 c5 c6 c7 c8 c9
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
2. alternative values in c3, c4, c5 and c6 such as
c1 c2 c3 c4 c5 c6 c7 c8 c9
0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
3. at least 1 among c7, c8 and c9
c1 c2 c3 c4 c5 c6 c7 c8 c9
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1 1
Please help me How i can code these constraints.
After getting all my upper constraints my final task is to combine all as given bellow.
there are three groups
1. c1 and c2
2. c3,c4,c5 and c6
3. c7,c8 and c9
Between groups there is OR operation. as given below
c1 c2 c3 c4 c5 c6 c7 c8 c9
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 0 1
1 0 0 1 0 0 1 1 0
Something like this
If I compute all possible solutions in this way. I get 119 unique combinations

  4 件のコメント

Asad Abbas 2016 年 9 月 6 日
Thank you so much for your interest. After getting these constraints I will make all combinations such as
c1 c2 c3 c4 c5 c6 c7 c8 c9
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 0 1
1 0 0 1 0 0 1 1 0
Something like this
If I compute all possible solutions in this way. I get 119 unique combinations
Stephen Cobeldick
2016 年 9 月 6 日
@Asad Abbas: your output does not seem to follow your constraints. You show that either c1 or c2 should have a 1, but most rows of your example this is not true, e.g. the third row 0 0 1 0 0 0 0 0 0. How your constraints relate to your output is not clear.
Asad Abbas 2016 年 9 月 6 日
@Stephen Cobeldick Sir there are three groups
1. c1 and c2
2. c3,c4,c5 and c6
3. c7,c8 and c9
Between groups there is or operation.

サインイン to comment.

1 件の回答

John D'Errico
回答者: John D'Errico
2016 年 9 月 6 日
 採用された回答

For example, are you asking to create a random matrix such that in each ROW, the elements in columns 1 and 2 must be either [0 1] or [1 0]?
If so, then just compute a matrix with the desired number of rows that have either of those two possibilities.
N = 119;
possibleSet = [0 1;1 0];
np = size(possibleSet,1);
M12 = possibleSet(ceil(rand(N,1)*np),:);
Next, create columns 3 through 6 separately, as desired. Combine with the first two columns.
If this is your goal, then just do it. But I have an inkling that you may have meant something entirely different, or that your real problem is far more complex.

  3 件のコメント

Asad Abbas 2016 年 9 月 6 日
Sir I have explained example of my all possible values in matrix under your 1st comment. Please have a look.
John D'Errico
2016 年 9 月 6 日
And I explained how to generate the solution. Please read my answer. Apply that same algorithm to columns 1&2 then 3,4,5,6, then 7,8,9. If you refuse to read when someone gives you a valid answer, then how will you ever get any answer?
Asad Abbas 2016 年 9 月 7 日
Thank you so much Sir. Its working now to follow your suggestions. I have got all 119 unique solutions.

サインイン to comment.



Translated by