0

Guys I have problem to make a random matrix by considering some constraint and another matrix.

I have matrix :

A= [ 1 4 3 5 2 6 8 7 10 11 12 9;
     4 1 2 3 6 5 9 8 7 10 11 12;
     1 2 3 4 5 6 12 9 8 10 11 7]

then, i want to make a matrix B (3,12) which has value between 0 and 2 ([ 0 2]) randomly. but there is some constraint :

a. no consecutive zeros (0) more than 2.
b. sum all element in each row in matrix B <=11,
c. if there is value 1 or 2 or 3 or 4 in coordinate (a,b) in matrix A. so we have to force in coordinate(a,b+1) in matrix B have value zero (0).

thanks for your help before guys.

Febri Dwi Laksono
  • 309
  • 1
  • 7
  • 15
  • 2
    considering your [other question](http://stackoverflow.com/questions/12000476/how-to-make-random-matrix-whose-rows-must-all-sum-11-and-which-cannot-contain) and the answers you got there, I think you should really be able to solve this problem by yourself now. I'd love to help, but this question is *so much* like the other ones you've asked, that I hardly think you're even making an effort. So: the first two constraints, you know how to do. **what have you tried** to get the last constraint in? – Rody Oldenhuis Aug 24 '12 at 07:28
  • @RodyOldenhuis : yeah the first and second I know because you mr. but really, i dont know when i have to compile three constraint above. you know I'am newbie right. I always try first before I ask here you know mr. but if I dont know how to solve, I will ask here. for the third constraint : if I use while looping, it will take long time. I just want to share to another (red:here), if there is person will help me, i will really thanks so much. just need help mr. actually i dont want ask another if I know how to solve. – Febri Dwi Laksono Aug 24 '12 at 07:36
  • 1
    For such small matrices any possible performance degradation that comes from using explicit looping statements, nested 2 or 3 or 4 deep, compared with a vectorised approach will be minuscule compared to the time you spend asking these questions. If you can achieve what you want with loops, then use them. If you then post a question such as *Can you help me improve the performance of these loops ?* you'd get much better answers than the question here will bring. Do you get the impression that SO is getting tired of writing your code for you and not seeing you doing more of your own work ? – High Performance Mark Aug 24 '12 at 08:49
  • @HighPerformanceMark: yah, I am sorry if I always ask you, oke thanks for your help and suggestion. – Febri Dwi Laksono Aug 24 '12 at 09:03

1 Answers1

0

So, if you use this answer, to generate a B which fulfills the constraints 1 and 2, you can use something similar to this

B = rand(3, 12); %# Just an example matrix.
idx = bitand(A >= 1, A <= 4);
B([false(size(idx, 1), 1) idx(:,1:end-1)]) = 0

to enforce the third constraint.

Community
  • 1
  • 1
Mehrwolf
  • 8,208
  • 2
  • 26
  • 38