How to generate a matrix with certain conditionsStandardizing a coset table via matrix...
Using loops to create tables
How do creatures spend Hit Dice after a short rest (if they can do so)?
Is it safe to try charging my laptop with a USB-C PD charger that has less wattage than recommended?
Word or phrase for showing great skill at something without formal training in it
Strange Sign on Lab Door
How can animals be objects of ethics without being subjects as well?
What is better: yes / no radio, or simple checkbox?
The vanishing of sum of coefficients: symmetric polynomials
Tikzing a circled star
Getting a UK passport renewed when you have dual nationality and a different name in your second country?
Why do neural networks need so many training examples to perform?
Slow moving projectiles from a hand-held weapon - how do they reach the target?
What incentive is there for baking on the test chain during voting period?
How Internet Communication Works on a Coaxial Cable
How should I handle players who ignore the session zero agreement?
Does Windows 10's telemetry include sending *.doc files if Word crashed?
What is this metal M-shaped device for?
just upgraded iMac late 2015 ram from 12 GB to 28 GB and it became too slow to use
What is the wife of a henpecked husband called?
How to join 2 tables in mysql but with different values in one of the tables
En Passant For Beginners
Everyone is beautiful
How did the original light saber work?
"On one hand" vs "on the one hand."
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
$begingroup$
I want to generate a $n times n$ matrix.
- I want the diagonal entries to be all 0
- I want a random choice of matrix elements with 0 or 1.
- 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
New contributor
$endgroup$
add a comment |
$begingroup$
I want to generate a $n times n$ matrix.
- I want the diagonal entries to be all 0
- I want a random choice of matrix elements with 0 or 1.
- 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
New contributor
$endgroup$
$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
6 hours ago
add a comment |
$begingroup$
I want to generate a $n times n$ matrix.
- I want the diagonal entries to be all 0
- I want a random choice of matrix elements with 0 or 1.
- 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
New contributor
$endgroup$
I want to generate a $n times n$ matrix.
- I want the diagonal entries to be all 0
- I want a random choice of matrix elements with 0 or 1.
- 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
matrix probability-or-statistics random sampling
New contributor
New contributor
edited 1 hour ago
J. M. is computer-less♦
97k10303463
97k10303463
New contributor
asked 6 hours ago
tiffanytiffany
61
61
New contributor
New contributor
$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
6 hours ago
add a comment |
$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
6 hours ago
$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
6 hours ago
$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
6 hours ago
add a comment |
3 Answers
3
active
oldest
votes
$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
You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.
$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
5 hours ago
1
$begingroup$
@tiffany, just doWith[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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)$
$endgroup$
$begingroup$
Altho using theBernoulliDistribution[]
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♦
1 hour ago
add a comment |
$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
$endgroup$
$begingroup$
Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
$endgroup$
– tiffany
6 hours ago
$begingroup$
@tiffany you setm
andn
in the first two lines...
$endgroup$
– Vsevolod A.
4 hours ago
1
$begingroup$
Justrnd := If[RandomReal[{0, 1}] < 1/m, 1, 0];
will do, since the function never uses its argument.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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
You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.
$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
5 hours ago
1
$begingroup$
@tiffany, just doWith[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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
You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.
$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
5 hours ago
1
$begingroup$
@tiffany, just doWith[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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
You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.
$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
You can change the 0.9 to any value (this is your m). The second line sets all the diagonal elements to zero.
answered 6 hours ago
bill sbill s
53.6k376153
53.6k376153
$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
5 hours ago
1
$begingroup$
@tiffany, just doWith[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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
5 hours ago
1
$begingroup$
@tiffany, just doWith[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.
$endgroup$
– J. M. is computer-less♦
1 hour 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
5 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
5 hours ago
1
1
$begingroup$
@tiffany, just do
With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.$endgroup$
– J. M. is computer-less♦
1 hour ago
$begingroup$
@tiffany, just do
With[{n = 8, m = 3}, (# - DiagonalMatrix[Diagonal[#]]) &[RandomVariate[BernoulliDistribution[1/m], {n, n}]]]
.$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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)$
$endgroup$
$begingroup$
Altho using theBernoulliDistribution[]
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♦
1 hour ago
add a comment |
$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)$
$endgroup$
$begingroup$
Altho using theBernoulliDistribution[]
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♦
1 hour ago
add a comment |
$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)$
$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)$
answered 4 hours ago
Okkes DulgerciOkkes Dulgerci
5,0891917
5,0891917
$begingroup$
Altho using theBernoulliDistribution[]
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♦
1 hour ago
add a comment |
$begingroup$
Altho using theBernoulliDistribution[]
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♦
1 hour 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♦
1 hour 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♦
1 hour ago
add a comment |
$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
$endgroup$
$begingroup$
Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
$endgroup$
– tiffany
6 hours ago
$begingroup$
@tiffany you setm
andn
in the first two lines...
$endgroup$
– Vsevolod A.
4 hours ago
1
$begingroup$
Justrnd := If[RandomReal[{0, 1}] < 1/m, 1, 0];
will do, since the function never uses its argument.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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
$endgroup$
$begingroup$
Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
$endgroup$
– tiffany
6 hours ago
$begingroup$
@tiffany you setm
andn
in the first two lines...
$endgroup$
– Vsevolod A.
4 hours ago
1
$begingroup$
Justrnd := If[RandomReal[{0, 1}] < 1/m, 1, 0];
will do, since the function never uses its argument.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$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
$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
answered 6 hours ago
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
6 hours ago
$begingroup$
@tiffany you setm
andn
in the first two lines...
$endgroup$
– Vsevolod A.
4 hours ago
1
$begingroup$
Justrnd := If[RandomReal[{0, 1}] < 1/m, 1, 0];
will do, since the function never uses its argument.
$endgroup$
– J. M. is computer-less♦
1 hour ago
add a comment |
$begingroup$
Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
$endgroup$
– tiffany
6 hours ago
$begingroup$
@tiffany you setm
andn
in the first two lines...
$endgroup$
– Vsevolod A.
4 hours ago
1
$begingroup$
Justrnd := If[RandomReal[{0, 1}] < 1/m, 1, 0];
will do, since the function never uses its argument.
$endgroup$
– J. M. is computer-less♦
1 hour ago
$begingroup$
Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
$endgroup$
– tiffany
6 hours ago
$begingroup$
Thank you Vsevolod A. How will the command be if I do not restrict what n and m be?
$endgroup$
– tiffany
6 hours ago
$begingroup$
@tiffany you set
m
and n
in the first two lines...$endgroup$
– Vsevolod A.
4 hours ago
$begingroup$
@tiffany you set
m
and n
in the first two lines...$endgroup$
– Vsevolod A.
4 hours ago
1
1
$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♦
1 hour 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♦
1 hour ago
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
For a start, your code seems to set the diagonal elements to 1 rather than zero.
$endgroup$
– MarcoB
6 hours ago