# How to put a FOR loop in a WHILE loop

I have a strange matrix like this one in the picture

Now, my goal is to change all the 1s with random complex numbers of modulus 1, but if the element $$a_{12}=z$$, then $$a_{21}= \bar{z}$$, so the complex conjugate of $$z$$.

In the code CAC is this starting matrix, PCAC is the "complex" matrix I want to create. Nn is the "size" of squares of zeros inside (5x5 in this case, so Nn=5).

PCAC = CAC;
j = 2;
While[j <= 2 Nn + 1,
θ = RandomReal[{0, 2 Pi}];
PCAC[[1, j]] = Exp[I θ];
PCAC[[j, 1]] = Exp[-I θ];
j = j + 1;
]
a = 2;
While[a <= Nn + 1;
For[i = 1, i <= Nn, i++,
γ = RandomReal[{0, 2 Pi}];
PCAC[[a, Nn + 1 + i]] = Exp[I γ];
PCAC[[Nn + 1 + i, a]] = Exp[-I γ];
];
a=a+1;
]
MatrixForm[PCAC]


My problem

With the first while loop, I fill the first row and the first column. Everything ok.

Then I fill with complex numbers the "squares inside" but it's like that the second while loop doesn't work (and the for loop works). Here there is what I get:

Why the while loop does not continue after changing the second row and column?

• Your second While[a<=Nn+1;For[...];a=a+1] I think that semicolon before the For should be a comma instead. As you have it I think the For and the a=a+1 are all part of the While condition.
– Bill
Commented Mar 18, 2022 at 19:27
• Yes, the problem was the comma instead of ; Thank you! Sorry for that stupid problem Commented Mar 18, 2022 at 19:43
• No sorry's, not for me. If I'd NEVER made a mistake like that then maybe. But with all the compute power we have sitting there idle, before we actually start running the code, doesn't it seem like it could be scanning the code in the background and up would pop a "purple bouncing fickle finger" showing us where there is likely something really really wrong that we just haven't noticed yet. Consider the kinds of common mistakes that most of us humans make and the machine doesn't bounce that finger on that telling us "look here, look here, this is really really likely a dumb mistake."
– Bill
Commented Mar 18, 2022 at 19:47
• This can be done more simply. I defined cac as Normal[...] where ... denotes the construction in the response by @Edmund. Then simply replace 1's and add the result to its conjugate transpose, like so. In[344]:= cacb = cac /. 1 :> Exp[2*I*Pi*RandomReal[]]; cacRandHerm = cacb + ConjugateTranspose[cacb]; HermitianMatrixQ[cacRandHerm] Out[346]= True Commented Mar 18, 2022 at 21:51

Create CAC.

CAC =
SparseArray[
{
{1, 1} -> 0
, Band[{2, 2}, {11, 11}] -> {ConstantArray[0, {5, 5}]}
}, {11, 11}, 1]


Position of 1's in upper triangular.

pos = Position[UpperTriangularize[CAC], 1];


Random complex numbers of modulus 1 in upper triangular.

PCAC = ReplacePart[CAC, Thread[pos -> Exp[I RandomReal[{0, 2 Pi}, Length@pos]]]];


Conjugate of upper triangular in lower triangular

With[{ut = UpperTriangularize[PCAC]},
PCAC = ut + Transpose[Conjugate[ut]]
];
MatrixForm[PCAC]


Hope this helps.

Complex number with a unity Norm:

rNormOne := Module[{\[Theta]},
\[Theta] = RandomReal[{0, 2 \[Pi]}];
r = Cos[\[Theta]] + I Sin[\[Theta]]
]

amat = {{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
, {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}
, {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}
, {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}
, {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}
, {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}
, {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
, {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
, {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
, {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
, {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
}


Check:

HermitianMatrixQ[amat]


(* True *)

LowerTriangularize and substitute a random Complex number for each 1 entry.

(lt = (LowerTriangularize[amat] /. 1 :> rNormOne)  /. (0. + 0. I) ->
0 // Chop) // MatrixForm


Generate upper triangular matrix:

(ut = ConjugateTranspose[lt]) // MatrixForm


Join:

(newMat = ut + lt) // MatrixForm

HermitianMatrixQ[newMat]


(* True *)

Map[Norm, newMat, {2}] // MatrixForm


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