필터 지우기
필터 지우기

I want to create a random matrix?

조회 수: 1 (최근 30일)
Pham
Pham 2013년 12월 29일
댓글: Pham 2013년 12월 30일
Help me!
I want to create a random matrix HM (3,5), it satisfies the following conditions:
1. The value of HM is a nonnegative integer from 0 to 4.
2. The total value of the elements in a row is less than or equal to 5.
Thank you very much!
  댓글 수: 3
Pham
Pham 2013년 12월 29일
Ok, sorry. a nonnegative integer from 0 to 4 with the total value of the elements in a row is less than or equal to 6.
Pham
Pham 2013년 12월 29일
you can help me

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

채택된 답변

Amit
Amit 2013년 12월 29일
okay .. an approach would be rejection method .. be careful .. for extremely large matrix this will be a bad idea .. tmp1= randi([0 c],a*100,b); tmp1=tmp1(sum(tmp1,2)<=d); HM=tmp1(1:a,:);
  댓글 수: 6
Pham
Pham 2013년 12월 29일
OK. Thanh you very much!
Pham
Pham 2013년 12월 30일
Hello
The code above has the following disadvantages: When b is large and d is small, the matrix tmp1 to satisfy the conditions will be the number of rows <a. So HM matrix will fail. For example:
tmp1= randi([0 3],5*10000,50);
tmp1=tmp1(sum(tmp1,2)<=10,:);
HM=tmp1(1:5,:)
It will error: Index exceeds matrix dimensions.
Are you using the loop, you can just help me. Thanks.

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

추가 답변 (1개)

Amit
Amit 2013년 12월 29일
write down all possible ways to get 3 or less than 3 using integers 0-4 like [1 0 0 0 0] [1 1 0 0 0] etc.... pick one of the matrix for each row (randomly) and then use randperm to reorder them.
  댓글 수: 13
Amit
Amit 2013년 12월 29일
does b, c and d have any correlation? like in the example case, you have b=c+1, d=c+1
Pham
Pham 2013년 12월 29일
b, c and d are any integers

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by