Scale Matrix
Earlier you saw that scaling could be expressed with these dot products:
$$\begin{aligned}
p'_x &= \begin{bmatrix}\textrm{factors}_x & 0 & 0\end{bmatrix} \cdot \mathbf{p} \\
p'_y &= \begin{bmatrix}0 & \textrm{factors}_y & 0\end{bmatrix} \cdot \mathbf{p} \\
p'_z &= \begin{bmatrix}0 & 0 & \textrm{factors}_z\end{bmatrix} \cdot \mathbf{p} \\
\end{aligned}$$
But then we added a homogeneous coordinate to \(\mathbf{p}\) to make translation work. For the matrix-based transformation machinery to be universal, the dot products used to scale must also recognize the homogeous coordinate. The coordinate does not contribute to \(p'_x\), \(p'_y\), or \(p'_z\), so its paired coefficients in the first three rows are 0:
$$\begin{aligned}
p'_x &= \begin{bmatrix}\textrm{factors}_x & 0 & 0 & 0\end{bmatrix} \cdot \mathbf{p} \\
p'_y &= \begin{bmatrix}0 & \textrm{factors}_y & 0 & 0\end{bmatrix} \cdot \mathbf{p} \\
p'_z &= \begin{bmatrix}0 & 0 & \textrm{factors}_z & 0\end{bmatrix} \cdot \mathbf{p} \\
\end{aligned}$$
As with translation, we add a fourth dot product to produce the homogeneous coordinate of \(\mathbf{p'}\):
$$\begin{aligned}
1 &= \begin{bmatrix}0 & 0 & 0 & 1\end{bmatrix} \cdot \mathbf{p}
\end{aligned}$$
All four dot products will be evaluated simultaneously when we assemble the vectors into this matrix:
$$\begin{aligned}
\begin{bmatrix}
\textrm{factors}_x & 0 & 0 & 0 \\
0 & \textrm{factors}_y & 0 & 0 \\
0 & 0 & \textrm{factors}_z & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
\end{aligned}$$
Scaling may now be expressed as a matrix-vector multiplication, just like translation:
$$\begin{aligned}
\begin{bmatrix}p'_x \\ p'_y \\ p'_z \\ 1\end{bmatrix} &= \begin{bmatrix}
\textrm{factors}_x & 0 & 0 & 0 \\
0 & \textrm{factors}_y & 0 & 0 \\
0 & 0 & \textrm{factors}_z & 0 \\
0 & 0 & 0 & 1
\end{bmatrix} \times \begin{bmatrix}p_x \\ p_y \\ p_z \\ 1\end{bmatrix} \\
\end{aligned}$$
All that remains is to figure out the matrix for rotation.