# Adding one row and column to a matrix

RotationMatrix[θ, {0, 0, 1}] // Matrixform

gives

$$\begin{pmatrix} \cos (\theta ) & -\sin (\theta ) & 0 \\ \sin (\theta ) & \cos (\theta ) & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}$$

Now, I would like to add a row and column to it like this

$$\begin{pmatrix} \color{red}{1}&\color{red}{0}&\color{red}{0}&\color{red}{0}\\ \color{red}{0}& \cos (\theta ) & -\sin (\theta ) & 0 \\ \color{red}{0}&\sin (\theta ) & \cos (\theta ) & 0 \\ \color{red}{0}& 0 & 0 & 1 \\ \end{pmatrix}$$

I would like to just be able to add red ones on any RotationMatrix[θ, w] that I define as done in the beginning.

Is there a simple way to achieve this?

On a similar note,

RotationMatrix[I ϕ, {0, 0, 1}] // MatrixForm

gives

$$\begin{pmatrix} \cosh (\phi ) & -i \sinh (\phi ) & 0 \\ i \sinh (\phi ) & \cosh (\phi ) & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}$$

So here I would like to add a row:

$$\begin{pmatrix}\cosh \phi & -i\sinh \phi & 0 & \color{red}0 \\ i\sinh \phi & \cosh \phi & 0 & \color{red}0 \\ 0 & 0 & 1 & \color{red}0 \\ \color{red}0 & \color{red}0 & \color{red}0 & \color{red}1\end{pmatrix}$$

And here also I would like to have the flexibility of defining the things in a black part about any vector. Any suggestions?

• Why couldn't you just use RotationMatrix[] directly? Look at e.g. RotationMatrix[θ, IdentityMatrix[[{2, 3}]]] and RotationMatrix[I ϕ, IdentityMatrix[[{1, 2}]]]. (See this as well.) Jan 29 at 7:45
• This is perfect. Thanks. Didn't know that I could use two vectors to define a higher dimension rotation like that. Jan 29 at 16:32

rm = RotationMatrix[θ, {0, 0, 1}];
rm2 = RotationMatrix[I θ, {0, 0, 1}];


### 1. ArrayFlatten

ArrayFlatten[{{1, 0}, {0, rm}}] // MatrixForm ArrayFlatten[{{rm2, 0}, {0, 1}}] // MatrixForm PadRight[{{1}}, {4, 4}] + PadLeft[rm, {4, 4}] // MatrixForm PadLeft[{{1}}, {4, 4}] + PadRight[rm2, {4, 4}] // MatrixForm You can also do

MatrixForm /@ {Prepend[UnitVector[4, 1]] @ PadLeft[rm, {3, 4}],
Append[UnitVector[4, 4]] @ PadRight[rm2, {3, 4}]} // Row


or

MatrixForm /@ {MapAt[1 &, {1, 1}]@PadLeft[rm, {4, 4}],
MapAt[1 &, {-1, -1}]@PadRight[rm2, {4, 4}]} // Row


or

MatrixForm /@ {ReplacePart[ {1, 1} -> 1] @ PadLeft[rm, {4, 4}],
ReplacePart[ {-1, -1} -> 1] @ PadRight[rm2, {4, 4}]} // Row


to get MatrixForm /@ {Prepend[UnitVector[4, 1]] @ ArrayPad[rm, {{0}, {1, 0}}],
Append[UnitVector[4, 4]] @ ArrayPad[rm2, {{0}, {0, 1}}]} // Row ### 4. SparseArraySparseBlockMatrix

SparseArraySparseBlockMatrix[{{1, 1} -> {{1}}, {2, 2} -> rm}] // MatrixForm SparseArraySparseBlockMatrix[{{1, 1} -> rm2, {2, 2} -> {{1}}}] // MatrixForm matrix = RotationMatrix[θ, {0, 0, 1}];
matrix1 = SparseArray[{Band[{1, 1}] -> {matrix, {{1}}}}];
matrix2 = SparseArray[{Band[{1, 1}] -> {{{1}}, matrix}}];
matrix // MatrixForm
matrix1 // MatrixForm
matrix2 // MatrixForm
`