Random number generation but alternative

Please help me. I want generate random number 0 and 1 as given code with constraints. Now my given constraint is 1st column and 2nd column not equal.
a=randi([0 1],24,7)
a(:,1)=~a(:,2)
e.g
0 1
1 0
How I can give the constraints in first 3 columns such as any two column become 0 and only one column should be 1? e.g.
0 0 1
1 0 0
0 1 0

답변 (1개)

John D'Errico
John D'Errico 2016년 9월 1일

1 개 추천

For each row, just pick a random column from the first k columns (where k is either 2 or 3, depending on your problem) to put a 1 in. Leave the other columns as 0. WTP?
If the other columns of your matrix have no constraints, then just generate them randomly.

댓글 수: 2

Asad Abbas
Asad Abbas 2016년 9월 1일
Thank you so much. Can you please explain more?
John D'Errico
John D'Errico 2016년 9월 1일
Oh, come on. You already know how to use randi. Use it to generate a random number from 1 to 3. If you have no idea how to do this in one vectorized operation, then use a loop. If you have no clue how to use even a loop, so basic MATLAB, then you need to read the tutorials.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2016년 9월 1일

댓글:

2016년 9월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by