Changing iteration variable in Do loopChanging a Part of a variable within ParallelDoIterating FindRoot to solve a differential equationBeginners problem, Do Loop, Eigenfunction iterationExecute a notebook with a changing variablesuccessive iterationHow do I use the values generated in a for-loop in my first iteration to my next iteration?While loop with changing variable , NDSolve and an IntegralHow do I repeat the number of times a nested for loop does an iteration?Evaluating number of iteration with a certain map with WhileThe variable in the While loop does not change
Changing iteration variable in Do loop
How do I reproduce this layout and typography?
Did Hitler say this quote about homeschooling?
Why don't humans perceive waves as twice the frequency they are?
Does 5e follow the Primary Source rule?
How long were the Apollo astronauts allowed to breathe 100% oxygen at 1 atmosphere continuously?
Will the internet speed decrease on second router if there are multiple devices connected to primary router?
How did J. J. Thomson establish the particle nature of the electron?
"This used to be my phone number"
Random piece of plastic
How to interpret a promising preprint that was never published in peer-review?
What is a Romeo Word™?
Manager asking me to eat breakfast from now on
Who would use the word "manky"?
Installing Older Version of MacOS/OSX alongside current
How do you send money when you're not sure it's not a scam?
What is the name for the average of the largest and the smallest values in a given data set?
What would be the safest way to drop thousands of small, hard objects from a typical, high wing, GA airplane?
Which modern firearm should a time traveler bring to be easily reproducible for a historic civilization?
Why doesn't Venus have a magnetic field? How does the speed of rotation affect the magnetic field of a planet?
literal `0` beeing a valid candidate for int and const string& overloads causes ambiguous call
Don't individual signal sources affect each other when using a summing amplifier?
In this iconic lunar orbit rendezvous photo of John Houbolt, why do arrows #5 and #6 point the "wrong" way?
How would you say "Sorry, that was a mistake on my part"?
Changing iteration variable in Do loop
Changing a Part of a variable within ParallelDoIterating FindRoot to solve a differential equationBeginners problem, Do Loop, Eigenfunction iterationExecute a notebook with a changing variablesuccessive iterationHow do I use the values generated in a for-loop in my first iteration to my next iteration?While loop with changing variable , NDSolve and an IntegralHow do I repeat the number of times a nested for loop does an iteration?Evaluating number of iteration with a certain map with WhileThe variable in the While loop does not change
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I want to generate random positive hermitian matrices, I start with such a code
rho11R = Table[RandomReal[], i, 1, 4];
rho10R = Table[RandomComplex[], i, 1, 4];
Do[If[rho11R[[i]] - rho11R[[i]]^2 -
rho10R[[i]]*Conjugate[rho10R[[i]]] >= 0,
Nothing, rho10R[[i]] = RandomComplex[], --i], i, 1,
4];
rhoR = Table[rho11R[[i]], rho10R[[i]], Conjugate[rho10R[[i]]],
1 - rho11R[[i]], i, 1, 4]
This does not work, as the matrices I get are in general not positive.
The problem is my If function, which does not seem to change the iteration variable i to i-1, when the condition for positivity is not fulfilled (so that I can get new random variable for rho10R[[I]] and again check the condition for positivity). How can I reach this?
procedural-programming
$endgroup$
add a comment |
$begingroup$
I want to generate random positive hermitian matrices, I start with such a code
rho11R = Table[RandomReal[], i, 1, 4];
rho10R = Table[RandomComplex[], i, 1, 4];
Do[If[rho11R[[i]] - rho11R[[i]]^2 -
rho10R[[i]]*Conjugate[rho10R[[i]]] >= 0,
Nothing, rho10R[[i]] = RandomComplex[], --i], i, 1,
4];
rhoR = Table[rho11R[[i]], rho10R[[i]], Conjugate[rho10R[[i]]],
1 - rho11R[[i]], i, 1, 4]
This does not work, as the matrices I get are in general not positive.
The problem is my If function, which does not seem to change the iteration variable i to i-1, when the condition for positivity is not fulfilled (so that I can get new random variable for rho10R[[I]] and again check the condition for positivity). How can I reach this?
procedural-programming
$endgroup$
$begingroup$
Have you tried aWhile[]
loop instead?
$endgroup$
– Somos
8 hours ago
$begingroup$
I assume that by positive you mean positive semi-definite, right?
$endgroup$
– Roman
7 hours ago
add a comment |
$begingroup$
I want to generate random positive hermitian matrices, I start with such a code
rho11R = Table[RandomReal[], i, 1, 4];
rho10R = Table[RandomComplex[], i, 1, 4];
Do[If[rho11R[[i]] - rho11R[[i]]^2 -
rho10R[[i]]*Conjugate[rho10R[[i]]] >= 0,
Nothing, rho10R[[i]] = RandomComplex[], --i], i, 1,
4];
rhoR = Table[rho11R[[i]], rho10R[[i]], Conjugate[rho10R[[i]]],
1 - rho11R[[i]], i, 1, 4]
This does not work, as the matrices I get are in general not positive.
The problem is my If function, which does not seem to change the iteration variable i to i-1, when the condition for positivity is not fulfilled (so that I can get new random variable for rho10R[[I]] and again check the condition for positivity). How can I reach this?
procedural-programming
$endgroup$
I want to generate random positive hermitian matrices, I start with such a code
rho11R = Table[RandomReal[], i, 1, 4];
rho10R = Table[RandomComplex[], i, 1, 4];
Do[If[rho11R[[i]] - rho11R[[i]]^2 -
rho10R[[i]]*Conjugate[rho10R[[i]]] >= 0,
Nothing, rho10R[[i]] = RandomComplex[], --i], i, 1,
4];
rhoR = Table[rho11R[[i]], rho10R[[i]], Conjugate[rho10R[[i]]],
1 - rho11R[[i]], i, 1, 4]
This does not work, as the matrices I get are in general not positive.
The problem is my If function, which does not seem to change the iteration variable i to i-1, when the condition for positivity is not fulfilled (so that I can get new random variable for rho10R[[I]] and again check the condition for positivity). How can I reach this?
procedural-programming
procedural-programming
asked 8 hours ago
AgnieszkaAgnieszka
10410 bronze badges
10410 bronze badges
$begingroup$
Have you tried aWhile[]
loop instead?
$endgroup$
– Somos
8 hours ago
$begingroup$
I assume that by positive you mean positive semi-definite, right?
$endgroup$
– Roman
7 hours ago
add a comment |
$begingroup$
Have you tried aWhile[]
loop instead?
$endgroup$
– Somos
8 hours ago
$begingroup$
I assume that by positive you mean positive semi-definite, right?
$endgroup$
– Roman
7 hours ago
$begingroup$
Have you tried a
While[]
loop instead?$endgroup$
– Somos
8 hours ago
$begingroup$
Have you tried a
While[]
loop instead?$endgroup$
– Somos
8 hours ago
$begingroup$
I assume that by positive you mean positive semi-definite, right?
$endgroup$
– Roman
7 hours ago
$begingroup$
I assume that by positive you mean positive semi-definite, right?
$endgroup$
– Roman
7 hours ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
This is one the cases where For
can be helpful (generally it's just more complicated.) With For
, you can manipulate the iteration variable, which you cannot do with Do
.
Example:
For[
i = 1,
i <= 4,
i++,
If[
i == 2,
Print[i++],
Print[i]
]
]
This prints 1
, 2
, 4
.
Another option is to use While
, as Somos wrote in a comment.
$endgroup$
add a comment |
$begingroup$
A much easier way to construct random positive semi-definite Hermitian matrices is to start with Gaussian random matrices and Hermitian-square them:
randommatrix[n_Integer?Positive] :=
RandomVariate[NormalDistribution[], n, n, 2].1, I
randomHermitian[n_Integer?Positive] :=
ConjugateTranspose[#].# & @ randommatrix[n]
In this way you don't need to reject anything.
Test:
randomHermitian[10] // Eigenvalues
(* 71.4553, 53.6575, 46.3275, 31.8263, 21.4754,
12.9687, 7.36107, 4.40568, 1.23665, 0.199904 *)
There is of course the question of the measure (distribution) from which you pull the random matrices; you'd have to be more specific in your question to address this point.
$endgroup$
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
add a comment |
$begingroup$
The following is a more "functional" way of doing acceptance/rejection
mtx := RandomComplex[1+I, 2, 2]
pmtx := Module[m = mtx, m1, m1 = Chop[m + ConjugateTranspose[m]];
If[PositiveDefiniteMatrixQ[m1], m1, pmtx]]
You can then generate a list of positive definite matrices of any length e.g.
Table[pmtx, 20]
(Note: I'm not addressing the question of whether this gives an appropriate distribution of random matrices)
$endgroup$
add a comment |
Your Answer
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
);
);
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%2f202385%2fchanging-iteration-variable-in-do-loop%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$
This is one the cases where For
can be helpful (generally it's just more complicated.) With For
, you can manipulate the iteration variable, which you cannot do with Do
.
Example:
For[
i = 1,
i <= 4,
i++,
If[
i == 2,
Print[i++],
Print[i]
]
]
This prints 1
, 2
, 4
.
Another option is to use While
, as Somos wrote in a comment.
$endgroup$
add a comment |
$begingroup$
This is one the cases where For
can be helpful (generally it's just more complicated.) With For
, you can manipulate the iteration variable, which you cannot do with Do
.
Example:
For[
i = 1,
i <= 4,
i++,
If[
i == 2,
Print[i++],
Print[i]
]
]
This prints 1
, 2
, 4
.
Another option is to use While
, as Somos wrote in a comment.
$endgroup$
add a comment |
$begingroup$
This is one the cases where For
can be helpful (generally it's just more complicated.) With For
, you can manipulate the iteration variable, which you cannot do with Do
.
Example:
For[
i = 1,
i <= 4,
i++,
If[
i == 2,
Print[i++],
Print[i]
]
]
This prints 1
, 2
, 4
.
Another option is to use While
, as Somos wrote in a comment.
$endgroup$
This is one the cases where For
can be helpful (generally it's just more complicated.) With For
, you can manipulate the iteration variable, which you cannot do with Do
.
Example:
For[
i = 1,
i <= 4,
i++,
If[
i == 2,
Print[i++],
Print[i]
]
]
This prints 1
, 2
, 4
.
Another option is to use While
, as Somos wrote in a comment.
answered 7 hours ago
C. E.C. E.
53.3k3 gold badges102 silver badges210 bronze badges
53.3k3 gold badges102 silver badges210 bronze badges
add a comment |
add a comment |
$begingroup$
A much easier way to construct random positive semi-definite Hermitian matrices is to start with Gaussian random matrices and Hermitian-square them:
randommatrix[n_Integer?Positive] :=
RandomVariate[NormalDistribution[], n, n, 2].1, I
randomHermitian[n_Integer?Positive] :=
ConjugateTranspose[#].# & @ randommatrix[n]
In this way you don't need to reject anything.
Test:
randomHermitian[10] // Eigenvalues
(* 71.4553, 53.6575, 46.3275, 31.8263, 21.4754,
12.9687, 7.36107, 4.40568, 1.23665, 0.199904 *)
There is of course the question of the measure (distribution) from which you pull the random matrices; you'd have to be more specific in your question to address this point.
$endgroup$
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
add a comment |
$begingroup$
A much easier way to construct random positive semi-definite Hermitian matrices is to start with Gaussian random matrices and Hermitian-square them:
randommatrix[n_Integer?Positive] :=
RandomVariate[NormalDistribution[], n, n, 2].1, I
randomHermitian[n_Integer?Positive] :=
ConjugateTranspose[#].# & @ randommatrix[n]
In this way you don't need to reject anything.
Test:
randomHermitian[10] // Eigenvalues
(* 71.4553, 53.6575, 46.3275, 31.8263, 21.4754,
12.9687, 7.36107, 4.40568, 1.23665, 0.199904 *)
There is of course the question of the measure (distribution) from which you pull the random matrices; you'd have to be more specific in your question to address this point.
$endgroup$
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
add a comment |
$begingroup$
A much easier way to construct random positive semi-definite Hermitian matrices is to start with Gaussian random matrices and Hermitian-square them:
randommatrix[n_Integer?Positive] :=
RandomVariate[NormalDistribution[], n, n, 2].1, I
randomHermitian[n_Integer?Positive] :=
ConjugateTranspose[#].# & @ randommatrix[n]
In this way you don't need to reject anything.
Test:
randomHermitian[10] // Eigenvalues
(* 71.4553, 53.6575, 46.3275, 31.8263, 21.4754,
12.9687, 7.36107, 4.40568, 1.23665, 0.199904 *)
There is of course the question of the measure (distribution) from which you pull the random matrices; you'd have to be more specific in your question to address this point.
$endgroup$
A much easier way to construct random positive semi-definite Hermitian matrices is to start with Gaussian random matrices and Hermitian-square them:
randommatrix[n_Integer?Positive] :=
RandomVariate[NormalDistribution[], n, n, 2].1, I
randomHermitian[n_Integer?Positive] :=
ConjugateTranspose[#].# & @ randommatrix[n]
In this way you don't need to reject anything.
Test:
randomHermitian[10] // Eigenvalues
(* 71.4553, 53.6575, 46.3275, 31.8263, 21.4754,
12.9687, 7.36107, 4.40568, 1.23665, 0.199904 *)
There is of course the question of the measure (distribution) from which you pull the random matrices; you'd have to be more specific in your question to address this point.
answered 7 hours ago
RomanRoman
14.3k1 gold badge19 silver badges51 bronze badges
14.3k1 gold badge19 silver badges51 bronze badges
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
add a comment |
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
$begingroup$
While the matrices you generate are Hermitian by construction, they may not be exactly, because of rounding errors. It is good practice to force to Hermitian, by averaging with the conjugate transpose.
$endgroup$
– mikado
1 hour ago
add a comment |
$begingroup$
The following is a more "functional" way of doing acceptance/rejection
mtx := RandomComplex[1+I, 2, 2]
pmtx := Module[m = mtx, m1, m1 = Chop[m + ConjugateTranspose[m]];
If[PositiveDefiniteMatrixQ[m1], m1, pmtx]]
You can then generate a list of positive definite matrices of any length e.g.
Table[pmtx, 20]
(Note: I'm not addressing the question of whether this gives an appropriate distribution of random matrices)
$endgroup$
add a comment |
$begingroup$
The following is a more "functional" way of doing acceptance/rejection
mtx := RandomComplex[1+I, 2, 2]
pmtx := Module[m = mtx, m1, m1 = Chop[m + ConjugateTranspose[m]];
If[PositiveDefiniteMatrixQ[m1], m1, pmtx]]
You can then generate a list of positive definite matrices of any length e.g.
Table[pmtx, 20]
(Note: I'm not addressing the question of whether this gives an appropriate distribution of random matrices)
$endgroup$
add a comment |
$begingroup$
The following is a more "functional" way of doing acceptance/rejection
mtx := RandomComplex[1+I, 2, 2]
pmtx := Module[m = mtx, m1, m1 = Chop[m + ConjugateTranspose[m]];
If[PositiveDefiniteMatrixQ[m1], m1, pmtx]]
You can then generate a list of positive definite matrices of any length e.g.
Table[pmtx, 20]
(Note: I'm not addressing the question of whether this gives an appropriate distribution of random matrices)
$endgroup$
The following is a more "functional" way of doing acceptance/rejection
mtx := RandomComplex[1+I, 2, 2]
pmtx := Module[m = mtx, m1, m1 = Chop[m + ConjugateTranspose[m]];
If[PositiveDefiniteMatrixQ[m1], m1, pmtx]]
You can then generate a list of positive definite matrices of any length e.g.
Table[pmtx, 20]
(Note: I'm not addressing the question of whether this gives an appropriate distribution of random matrices)
answered 2 hours ago
mikadomikado
7,2901 gold badge9 silver badges29 bronze badges
7,2901 gold badge9 silver badges29 bronze badges
add a comment |
add a comment |
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%2f202385%2fchanging-iteration-variable-in-do-loop%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$
Have you tried a
While[]
loop instead?$endgroup$
– Somos
8 hours ago
$begingroup$
I assume that by positive you mean positive semi-definite, right?
$endgroup$
– Roman
7 hours ago