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

Wayne King
Wayne King 2013년 12월 29일
편집: Wayne King 2013년 12월 29일
0 is not a positive integer. And why a nonnegative integer from 0 to 4? How can you select a 4 and possibly get a sum less than or equal to 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일

0 개 추천

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

Amit
Amit 2013년 12월 29일
the idea here is to create a large matrix(that's why 100*a) and the reject the values which don't qualify the conditions ..
Pham
Pham 2013년 12월 29일
When I for example: a = 5, b = 8, c = 4, d = 8, the matrix size is HM (5,1)?
Amit
Amit 2013년 12월 29일
you can also use while loop as well for rejection method, but that will be slower..
Amit
Amit 2013년 12월 29일
correction: tmp1=tmp1(sum(tmp1,2)<=d,:)
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일

0 개 추천

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일
편집: Amit 2013년 12월 29일
lets say you get 20 possible matrixes stored in comb(20,5) then:
temp1 = randi (20,10,1); HM=comb(temp1,:); for I=1:10 HM(I,:)=HM(I,randperm(5)); end
Sorry, I couldn't change it to code tags as I was replying from phone.
Pham
Pham 2013년 12월 29일
You can write a script to perform this matrix with 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일
Undefined variable comb.
Error in Untitled2 (line 2)
HM=comb(temp1,:);
Amit
Amit 2013년 12월 29일
편집: Amit 2013년 12월 29일
comb is the matrix you'll make. comb in your case will be [1 0 0 0 0; 1 1 0 0 0; 1 1 1 0 0;2 0 0 0 0; 2 1 0 0 0;3 0 0 0 0]
Pham
Pham 2013년 12월 29일
Thank you, but it looks like this is complex and crafts. You can just give me the brief command to create this matrix.
Amit
Amit 2013년 12월 29일
편집: Amit 2013년 12월 29일
the final code for your case:
comb=[1 0 0 0 0;1 1 0 0 0;1 1 1 0 0;2 0 0 0 0;2 1 0 0 0;3 0 0 0 0]; temp1 = randi (6,10,1); HM=comb(temp1,:); for I=1:10 HM(I,:)=HM(I,randperm(5)); end
Pham
Pham 2013년 12월 29일
You have way without using comb because the matrix HM large size, you can not list all cases of comb.
Amit
Amit 2013년 12월 29일
you changed the question from original post and how is 3×5 matrix matrix huge? and to make comb matrix, just make a code based on how you'll do it if you were suppose to do this on paper.
Pham
Pham 2013년 12월 29일
Because the matrix in my program HM large size. I just gave an example of a small matrix HM with such constraints.
Amit
Amit 2013년 12월 29일
Then can you state your real problem please?
Pham
Pham 2013년 12월 29일
편집: Pham 2013년 12월 29일
I want to create a random matrix HM (a,b), it satisfies the following conditions:
1. The value of HM is a nonnegative integer from 0 to c.
2. The total value of the elements in a row is less than or equal to d.
Above example is only one case.
Can you help me. Thank you very much!
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

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2013년 12월 29일

댓글:

2013년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by