3
$\begingroup$

I want to create a binary matrix of 6x3 in which six elements (m13,m23, m33, m42, m52, m61) are constant and assigned '0' value; whereas for other twelve elements (m11, m12, m21, m22, m31,m32, m41, m43, m51, m53, m62, m63) I want to assign value of "1" with the probability of 0.5. Given below my effort, I had made yet.

mat = Table[Subscript[m, i, j], {i, 6}, {j, 3}];
mat // MatrixForm
n = Round[18*0.5];
k = RandomSample[{mat[[1, 1]], mat[[1, 2]], mat[[1, 3]], mat[[2, 1]], 
   mat[[2, 2]], mat[[2, 3]], mat[[3, 1]], mat[[3, 2]], mat[[3, 3]], 
   mat[[4, 1]], mat[[4, 2]], mat[[4, 3]], mat[[5, 1]], mat[[5, 2]], 
   mat[[5, 3]], mat[[6, 1]], mat[[6, 2]], mat[[6, 3]]}, n]
$\endgroup$
  • 1
    $\begingroup$ See SparseArray, RandomInteger, Condition and MemberQ $\endgroup$ – Dr. belisarius Jan 6 '16 at 20:45
7
$\begingroup$
SparseArray[{i_, j_} :>  RandomInteger[{0, 1}] /; j != IntegerLength[7 - i, 2], 
            {6, 3}, a] // MatrixForm

Mathematica graphics

$\endgroup$
  • $\begingroup$ Kindly explain it briefly. $\endgroup$ – Mamoona Jan 6 '16 at 21:34
  • $\begingroup$ @Mamoona Please read the docs for each function and then ask specifically about the ones you don't understand $\endgroup$ – Dr. belisarius Jan 6 '16 at 23:48
2
$\begingroup$

One approach to this problem is to populate the whole matrix 0/1 with equal probabily, then set the desired elements to zero:

ReplacePart[ 
  RandomInteger[1 , {6, 3}] ,
     {{1, 3} , {2, 3} , {3, 3}, {4, 2}, {5, 2}, {6,1}} -> 0 ] // MatrixForm
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.