How to generate a matrix with certain conditionsStandardizing a coset table via matrix...

Finding the number of integers that are a square and a cube at the same time

Do authors have to be politically correct in article-writing?

Math reviews in "Zentralblatt für Mathematik und ihre Grenzgebiete"

No rhyme nor reason

A flower in a hexagon

Am I a Rude Number?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Superposition of light waves of different colors

Why does the DC-9-80 have this cusp in its fuselage?

Activating a Alphanet Faucet Wallet Remotely (without tezos-client)

How would an AI self awareness kill switch work?

How to properly claim credit for peer review?

raspberry pi change directory (cd) command not working with USB drive

Why is c4 a better move in this position?

Are there instances of considering any Goddess as a female lover?

Where is this triangular-shaped space station from?

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?

How can I use pgf from within TikZ

What is the wife of a henpecked husband called?

If I delete my router's history can my ISP still provide it to my parents?

Is life not blessing or mystery but suffering in Buddhism?

Copy large number of files of specific date to another directory?

4 Spheres all touching each other??

How do you enable SQL Server 2019's result set caching?



How to generate a matrix with certain conditions


Standardizing a coset table via matrix manipulationEigenvalues and Determinant of a large matrixHow to generate a random matrix with specific parameters?Generate Non-Singular Matrix of $ntimes n$ dimensionHow to create a matrix with some conditions?Problems with RandomChoicePicking first row of a matrix with a positive first elementSimplest way to construct a matrix its elements are defined by known functionsmatrix with chosen elements distributed in a random positionUsing Table to build a matrix













2












$begingroup$


I want to generate a $n times n$ matrix.




  1. I want the diagonal entries to be all 0

  2. I want a random choice of matrix elements with 0 or 1.

  3. The probability of having a 1 as a matrix element is $1/m$ and the probability of having a 0 as a matrix element is $1-1/m$.


I used the following command but it is wrong.



A[n_, m_] :=Table[If[i == j, 0,RandomVariate[BernoulliDistribution[m],{n,n}]]]


And I tried to test this command with n=4, m=0.4 but it didn't work.



Could anyone kindly tell me how to do this please?
Thank you!










share|improve this question









New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    For a start, your code seems to set the diagonal elements to 1 rather than zero.
    $endgroup$
    – MarcoB
    yesterday










  • $begingroup$
    Same question posted here.
    $endgroup$
    – Rohit Namjoshi
    20 hours ago










  • $begingroup$
    tiffany, please go here to get your accounts merged, so you can easily access your question.
    $endgroup$
    – J. M. is computer-less
    16 hours ago
















2












$begingroup$


I want to generate a $n times n$ matrix.




  1. I want the diagonal entries to be all 0

  2. I want a random choice of matrix elements with 0 or 1.

  3. The probability of having a 1 as a matrix element is $1/m$ and the probability of having a 0 as a matrix element is $1-1/m$.


I used the following command but it is wrong.



A[n_, m_] :=Table[If[i == j, 0,RandomVariate[BernoulliDistribution[m],{n,n}]]]


And I tried to test this command with n=4, m=0.4 but it didn't work.



Could anyone kindly tell me how to do this please?
Thank you!










share|improve this question









New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    For a start, your code seems to set the diagonal elements to 1 rather than zero.
    $endgroup$
    – MarcoB
    yesterday










  • $begingroup$
    Same question posted here.
    $endgroup$
    – Rohit Namjoshi
    20 hours ago










  • $begingroup$
    tiffany, please go here to get your accounts merged, so you can easily access your question.
    $endgroup$
    – J. M. is computer-less
    16 hours ago














2












2








2





$begingroup$


I want to generate a $n times n$ matrix.




  1. I want the diagonal entries to be all 0

  2. I want a random choice of matrix elements with 0 or 1.

  3. The probability of having a 1 as a matrix element is $1/m$ and the probability of having a 0 as a matrix element is $1-1/m$.


I used the following command but it is wrong.



A[n_, m_] :=Table[If[i == j, 0,RandomVariate[BernoulliDistribution[m],{n,n}]]]


And I tried to test this command with n=4, m=0.4 but it didn't work.



Could anyone kindly tell me how to do this please?
Thank you!










share|improve this question









New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I want to generate a $n times n$ matrix.




  1. I want the diagonal entries to be all 0

  2. I want a random choice of matrix elements with 0 or 1.

  3. The probability of having a 1 as a matrix element is $1/m$ and the probability of having a 0 as a matrix element is $1-1/m$.


I used the following command but it is wrong.



A[n_, m_] :=Table[If[i == j, 0,RandomVariate[BernoulliDistribution[m],{n,n}]]]


And I tried to test this command with n=4, m=0.4 but it didn't work.



Could anyone kindly tell me how to do this please?
Thank you!







matrix probability-or-statistics random sampling






share|improve this question









New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 hours ago









J. M. is computer-less

97.1k10303463




97.1k10303463






New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









tiffanytiffany

111




111




New contributor




tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






tiffany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    For a start, your code seems to set the diagonal elements to 1 rather than zero.
    $endgroup$
    – MarcoB
    yesterday










  • $begingroup$
    Same question posted here.
    $endgroup$
    – Rohit Namjoshi
    20 hours ago










  • $begingroup$
    tiffany, please go here to get your accounts merged, so you can easily access your question.
    $endgroup$
    – J. M. is computer-less
    16 hours ago


















  • $begingroup$
    For a start, your code seems to set the diagonal elements to 1 rather than zero.
    $endgroup$
    – MarcoB
    yesterday










  • $begingroup$
    Same question posted here.
    $endgroup$
    – Rohit Namjoshi
    20 hours ago










  • $begingroup$
    tiffany, please go here to get your accounts merged, so you can easily access your question.
    $endgroup$
    – J. M. is computer-less
    16 hours ago
















$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
yesterday




$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
yesterday












$begingroup$
Same question posted here.
$endgroup$
– Rohit Namjoshi
20 hours ago




$begingroup$
Same question posted here.
$endgroup$
– Rohit Namjoshi
20 hours ago












$begingroup$
tiffany, please go here to get your accounts merged, so you can easily access your question.
$endgroup$
– J. M. is computer-less
16 hours ago




$begingroup$
tiffany, please go here to get your accounts merged, so you can easily access your question.
$endgroup$
– J. M. is computer-less
16 hours ago










4 Answers
4






active

oldest

votes


















5












$begingroup$

Binary random variables are often modeled using the BernoulliDistribution. You can use the function RandomVariate to get a matrix of such variables.



mat = RandomVariate[BernoulliDistribution[0.9], {5, 5}]; 
mat - DiagonalMatrix[Diagonal[mat]]
% // MatrixForm


$$begin{pmatrix}
0&1&1&1&1\
1&0&1&1&1\
1&1&0&1&1\
1&0&1&0&1\
0&1&1&1&0end{pmatrix}$$



You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.



It's easy enough to make this into a function:



makeMat[n_, m_] := (mat = RandomVariate[BernoulliDistribution[m], {n, n}]; 
mat - DiagonalMatrix[Diagonal[mat]])


Then the above example is makeMat[5, 0.9]






share|improve this answer











$endgroup$













  • $begingroup$
    Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
    $endgroup$
    – tiffany
    yesterday






  • 2




    $begingroup$
    @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
    $endgroup$
    – J. M. is computer-less
    22 hours ago



















4












$begingroup$

You can use weight option in RandomChoice



n = 5;
m = 2;
mat = RandomChoice[{1/m, 1 - 1/m} -> {1, 0}, {n, n}];
(mat - DiagonalMatrix[Diagonal@mat]) // MatrixForm



$left(
begin{array}{ccccc}
0 & 1 & 1 & 0 & 1 \
1 & 0 & 1 & 0 & 1 \
1 & 0 & 0 & 0 & 1 \
0 & 0 & 1 & 0 & 0 \
0 & 0 & 0 & 1 & 0 \
end{array}
right)$







share|improve this answer









$endgroup$













  • $begingroup$
    Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
    $endgroup$
    – J. M. is computer-less
    22 hours ago



















2












$begingroup$

Since all the simple answers have been given, here is a SparseArray[] solution that may be useful if you want to generate large matrices without storing unneeded zero entries:



tiffany[n_Integer?Positive, m_] :=
SparseArray[{j_, k_} /; j != k && RandomReal[] < 1/m :> 1, {n, n}]


As an example:



BlockRandom[SeedRandom["tiffany"]; tiffany[7, 2.5] // Normal]
{{0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0}}





share|improve this answer









$endgroup$





















    0












    $begingroup$

    m = 2;
    n = 5;
    rnd[x_] := If[RandomReal[{0, 1}] < 1/m, 1, 0];
    t = Table[rnd[x_]*(1 - KroneckerDelta[i, j]), {i, 1, n}, {j, 1, n}];
    t // MatrixForm





    share|improve this answer









    $endgroup$













    • $begingroup$
      Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
      $endgroup$
      – tiffany
      yesterday










    • $begingroup$
      @tiffany you set m and n in the first two lines...
      $endgroup$
      – Vsevolod A.
      yesterday






    • 2




      $begingroup$
      Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
      $endgroup$
      – J. M. is computer-less
      22 hours ago











    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    tiffany is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192468%2fhow-to-generate-a-matrix-with-certain-conditions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5












    $begingroup$

    Binary random variables are often modeled using the BernoulliDistribution. You can use the function RandomVariate to get a matrix of such variables.



    mat = RandomVariate[BernoulliDistribution[0.9], {5, 5}]; 
    mat - DiagonalMatrix[Diagonal[mat]]
    % // MatrixForm


    $$begin{pmatrix}
    0&1&1&1&1\
    1&0&1&1&1\
    1&1&0&1&1\
    1&0&1&0&1\
    0&1&1&1&0end{pmatrix}$$



    You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.



    It's easy enough to make this into a function:



    makeMat[n_, m_] := (mat = RandomVariate[BernoulliDistribution[m], {n, n}]; 
    mat - DiagonalMatrix[Diagonal[mat]])


    Then the above example is makeMat[5, 0.9]






    share|improve this answer











    $endgroup$













    • $begingroup$
      Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
      $endgroup$
      – tiffany
      yesterday






    • 2




      $begingroup$
      @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
      $endgroup$
      – J. M. is computer-less
      22 hours ago
















    5












    $begingroup$

    Binary random variables are often modeled using the BernoulliDistribution. You can use the function RandomVariate to get a matrix of such variables.



    mat = RandomVariate[BernoulliDistribution[0.9], {5, 5}]; 
    mat - DiagonalMatrix[Diagonal[mat]]
    % // MatrixForm


    $$begin{pmatrix}
    0&1&1&1&1\
    1&0&1&1&1\
    1&1&0&1&1\
    1&0&1&0&1\
    0&1&1&1&0end{pmatrix}$$



    You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.



    It's easy enough to make this into a function:



    makeMat[n_, m_] := (mat = RandomVariate[BernoulliDistribution[m], {n, n}]; 
    mat - DiagonalMatrix[Diagonal[mat]])


    Then the above example is makeMat[5, 0.9]






    share|improve this answer











    $endgroup$













    • $begingroup$
      Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
      $endgroup$
      – tiffany
      yesterday






    • 2




      $begingroup$
      @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
      $endgroup$
      – J. M. is computer-less
      22 hours ago














    5












    5








    5





    $begingroup$

    Binary random variables are often modeled using the BernoulliDistribution. You can use the function RandomVariate to get a matrix of such variables.



    mat = RandomVariate[BernoulliDistribution[0.9], {5, 5}]; 
    mat - DiagonalMatrix[Diagonal[mat]]
    % // MatrixForm


    $$begin{pmatrix}
    0&1&1&1&1\
    1&0&1&1&1\
    1&1&0&1&1\
    1&0&1&0&1\
    0&1&1&1&0end{pmatrix}$$



    You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.



    It's easy enough to make this into a function:



    makeMat[n_, m_] := (mat = RandomVariate[BernoulliDistribution[m], {n, n}]; 
    mat - DiagonalMatrix[Diagonal[mat]])


    Then the above example is makeMat[5, 0.9]






    share|improve this answer











    $endgroup$



    Binary random variables are often modeled using the BernoulliDistribution. You can use the function RandomVariate to get a matrix of such variables.



    mat = RandomVariate[BernoulliDistribution[0.9], {5, 5}]; 
    mat - DiagonalMatrix[Diagonal[mat]]
    % // MatrixForm


    $$begin{pmatrix}
    0&1&1&1&1\
    1&0&1&1&1\
    1&1&0&1&1\
    1&0&1&0&1\
    0&1&1&1&0end{pmatrix}$$



    You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.



    It's easy enough to make this into a function:



    makeMat[n_, m_] := (mat = RandomVariate[BernoulliDistribution[m], {n, n}]; 
    mat - DiagonalMatrix[Diagonal[mat]])


    Then the above example is makeMat[5, 0.9]







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 16 hours ago









    J. M. is computer-less

    97.1k10303463




    97.1k10303463










    answered yesterday









    bill sbill s

    53.7k376153




    53.7k376153












    • $begingroup$
      Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
      $endgroup$
      – tiffany
      yesterday






    • 2




      $begingroup$
      @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
      $endgroup$
      – J. M. is computer-less
      22 hours ago


















    • $begingroup$
      Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
      $endgroup$
      – tiffany
      yesterday






    • 2




      $begingroup$
      @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
      $endgroup$
      – J. M. is computer-less
      22 hours ago
















    $begingroup$
    Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
    $endgroup$
    – tiffany
    yesterday




    $begingroup$
    Thank you bill s How will the command be if I do not restrict what n and m be? For example, I would like to generate a set of commands which I can replace m and n easily by any number.
    $endgroup$
    – tiffany
    yesterday




    2




    2




    $begingroup$
    @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
    $endgroup$
    – J. M. is computer-less
    22 hours ago




    $begingroup$
    @tiffany, just do With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]].
    $endgroup$
    – J. M. is computer-less
    22 hours ago











    4












    $begingroup$

    You can use weight option in RandomChoice



    n = 5;
    m = 2;
    mat = RandomChoice[{1/m, 1 - 1/m} -> {1, 0}, {n, n}];
    (mat - DiagonalMatrix[Diagonal@mat]) // MatrixForm



    $left(
    begin{array}{ccccc}
    0 & 1 & 1 & 0 & 1 \
    1 & 0 & 1 & 0 & 1 \
    1 & 0 & 0 & 0 & 1 \
    0 & 0 & 1 & 0 & 0 \
    0 & 0 & 0 & 1 & 0 \
    end{array}
    right)$







    share|improve this answer









    $endgroup$













    • $begingroup$
      Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
      $endgroup$
      – J. M. is computer-less
      22 hours ago
















    4












    $begingroup$

    You can use weight option in RandomChoice



    n = 5;
    m = 2;
    mat = RandomChoice[{1/m, 1 - 1/m} -> {1, 0}, {n, n}];
    (mat - DiagonalMatrix[Diagonal@mat]) // MatrixForm



    $left(
    begin{array}{ccccc}
    0 & 1 & 1 & 0 & 1 \
    1 & 0 & 1 & 0 & 1 \
    1 & 0 & 0 & 0 & 1 \
    0 & 0 & 1 & 0 & 0 \
    0 & 0 & 0 & 1 & 0 \
    end{array}
    right)$







    share|improve this answer









    $endgroup$













    • $begingroup$
      Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
      $endgroup$
      – J. M. is computer-less
      22 hours ago














    4












    4








    4





    $begingroup$

    You can use weight option in RandomChoice



    n = 5;
    m = 2;
    mat = RandomChoice[{1/m, 1 - 1/m} -> {1, 0}, {n, n}];
    (mat - DiagonalMatrix[Diagonal@mat]) // MatrixForm



    $left(
    begin{array}{ccccc}
    0 & 1 & 1 & 0 & 1 \
    1 & 0 & 1 & 0 & 1 \
    1 & 0 & 0 & 0 & 1 \
    0 & 0 & 1 & 0 & 0 \
    0 & 0 & 0 & 1 & 0 \
    end{array}
    right)$







    share|improve this answer









    $endgroup$



    You can use weight option in RandomChoice



    n = 5;
    m = 2;
    mat = RandomChoice[{1/m, 1 - 1/m} -> {1, 0}, {n, n}];
    (mat - DiagonalMatrix[Diagonal@mat]) // MatrixForm



    $left(
    begin{array}{ccccc}
    0 & 1 & 1 & 0 & 1 \
    1 & 0 & 1 & 0 & 1 \
    1 & 0 & 0 & 0 & 1 \
    0 & 0 & 1 & 0 & 0 \
    0 & 0 & 0 & 1 & 0 \
    end{array}
    right)$








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    Okkes DulgerciOkkes Dulgerci

    5,1591917




    5,1591917












    • $begingroup$
      Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
      $endgroup$
      – J. M. is computer-less
      22 hours ago


















    • $begingroup$
      Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
      $endgroup$
      – J. M. is computer-less
      22 hours ago
















    $begingroup$
    Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
    $endgroup$
    – J. M. is computer-less
    22 hours ago




    $begingroup$
    Altho using the BernoulliDistribution[] is best, this is likely to be more easy to read for someone who is not accustomed to discrete probability distributions.
    $endgroup$
    – J. M. is computer-less
    22 hours ago











    2












    $begingroup$

    Since all the simple answers have been given, here is a SparseArray[] solution that may be useful if you want to generate large matrices without storing unneeded zero entries:



    tiffany[n_Integer?Positive, m_] :=
    SparseArray[{j_, k_} /; j != k && RandomReal[] < 1/m :> 1, {n, n}]


    As an example:



    BlockRandom[SeedRandom["tiffany"]; tiffany[7, 2.5] // Normal]
    {{0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1},
    {0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0, 1, 0, 0},
    {0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0}}





    share|improve this answer









    $endgroup$


















      2












      $begingroup$

      Since all the simple answers have been given, here is a SparseArray[] solution that may be useful if you want to generate large matrices without storing unneeded zero entries:



      tiffany[n_Integer?Positive, m_] :=
      SparseArray[{j_, k_} /; j != k && RandomReal[] < 1/m :> 1, {n, n}]


      As an example:



      BlockRandom[SeedRandom["tiffany"]; tiffany[7, 2.5] // Normal]
      {{0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1},
      {0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0, 1, 0, 0},
      {0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0}}





      share|improve this answer









      $endgroup$
















        2












        2








        2





        $begingroup$

        Since all the simple answers have been given, here is a SparseArray[] solution that may be useful if you want to generate large matrices without storing unneeded zero entries:



        tiffany[n_Integer?Positive, m_] :=
        SparseArray[{j_, k_} /; j != k && RandomReal[] < 1/m :> 1, {n, n}]


        As an example:



        BlockRandom[SeedRandom["tiffany"]; tiffany[7, 2.5] // Normal]
        {{0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1},
        {0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0, 1, 0, 0},
        {0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0}}





        share|improve this answer









        $endgroup$



        Since all the simple answers have been given, here is a SparseArray[] solution that may be useful if you want to generate large matrices without storing unneeded zero entries:



        tiffany[n_Integer?Positive, m_] :=
        SparseArray[{j_, k_} /; j != k && RandomReal[] < 1/m :> 1, {n, n}]


        As an example:



        BlockRandom[SeedRandom["tiffany"]; tiffany[7, 2.5] // Normal]
        {{0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1},
        {0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0, 1, 0, 0},
        {0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0}}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 16 hours ago









        J. M. is computer-lessJ. M. is computer-less

        97.1k10303463




        97.1k10303463























            0












            $begingroup$

            m = 2;
            n = 5;
            rnd[x_] := If[RandomReal[{0, 1}] < 1/m, 1, 0];
            t = Table[rnd[x_]*(1 - KroneckerDelta[i, j]), {i, 1, n}, {j, 1, n}];
            t // MatrixForm





            share|improve this answer









            $endgroup$













            • $begingroup$
              Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
              $endgroup$
              – tiffany
              yesterday










            • $begingroup$
              @tiffany you set m and n in the first two lines...
              $endgroup$
              – Vsevolod A.
              yesterday






            • 2




              $begingroup$
              Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
              $endgroup$
              – J. M. is computer-less
              22 hours ago
















            0












            $begingroup$

            m = 2;
            n = 5;
            rnd[x_] := If[RandomReal[{0, 1}] < 1/m, 1, 0];
            t = Table[rnd[x_]*(1 - KroneckerDelta[i, j]), {i, 1, n}, {j, 1, n}];
            t // MatrixForm





            share|improve this answer









            $endgroup$













            • $begingroup$
              Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
              $endgroup$
              – tiffany
              yesterday










            • $begingroup$
              @tiffany you set m and n in the first two lines...
              $endgroup$
              – Vsevolod A.
              yesterday






            • 2




              $begingroup$
              Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
              $endgroup$
              – J. M. is computer-less
              22 hours ago














            0












            0








            0





            $begingroup$

            m = 2;
            n = 5;
            rnd[x_] := If[RandomReal[{0, 1}] < 1/m, 1, 0];
            t = Table[rnd[x_]*(1 - KroneckerDelta[i, j]), {i, 1, n}, {j, 1, n}];
            t // MatrixForm





            share|improve this answer









            $endgroup$



            m = 2;
            n = 5;
            rnd[x_] := If[RandomReal[{0, 1}] < 1/m, 1, 0];
            t = Table[rnd[x_]*(1 - KroneckerDelta[i, j]), {i, 1, n}, {j, 1, n}];
            t // MatrixForm






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Vsevolod A.Vsevolod A.

            478211




            478211












            • $begingroup$
              Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
              $endgroup$
              – tiffany
              yesterday










            • $begingroup$
              @tiffany you set m and n in the first two lines...
              $endgroup$
              – Vsevolod A.
              yesterday






            • 2




              $begingroup$
              Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
              $endgroup$
              – J. M. is computer-less
              22 hours ago


















            • $begingroup$
              Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
              $endgroup$
              – tiffany
              yesterday










            • $begingroup$
              @tiffany you set m and n in the first two lines...
              $endgroup$
              – Vsevolod A.
              yesterday






            • 2




              $begingroup$
              Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
              $endgroup$
              – J. M. is computer-less
              22 hours ago
















            $begingroup$
            Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
            $endgroup$
            – tiffany
            yesterday




            $begingroup$
            Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
            $endgroup$
            – tiffany
            yesterday












            $begingroup$
            @tiffany you set m and n in the first two lines...
            $endgroup$
            – Vsevolod A.
            yesterday




            $begingroup$
            @tiffany you set m and n in the first two lines...
            $endgroup$
            – Vsevolod A.
            yesterday




            2




            2




            $begingroup$
            Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
            $endgroup$
            – J. M. is computer-less
            22 hours ago




            $begingroup$
            Just rnd := If[RandomReal[{0, 1}] < 1/m, 1, 0]; will do, since the function never uses its argument.
            $endgroup$
            – J. M. is computer-less
            22 hours ago










            tiffany is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            tiffany is a new contributor. Be nice, and check out our Code of Conduct.













            tiffany is a new contributor. Be nice, and check out our Code of Conduct.












            tiffany is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192468%2fhow-to-generate-a-matrix-with-certain-conditions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            As a Security Precaution, the user account has been locked The Next CEO of Stack OverflowMS...

            Список ссавців Італії Природоохоронні статуси | Список |...

            Українські прізвища Зміст Історичні відомості |...