We perform a decomposition
$$
p = p_\parallel + p_\perp
$$
where $p_\parallel$ is parallel to $q$ and $p_\perp$ is perpendicular to $q$.
Then
$$
p \cdot q
= (p_\parallel + p_\perp) \cdot q
= p_\parallel \cdot q + \underbrace{p_\perp \cdot q}_0
= p_\parallel \cdot q
$$
and
$$
p \cdot q
= p_\parallel \cdot q
= \lVert p_\parallel \rVert \lVert q \rVert s
$$
with $s = \cos \angle(p_\parallel,q) \in \{-1, 1\}$ so we have
$$
\lVert p_\parallel \rVert s
= \frac{p \cdot q}{\lVert q \rVert}
$$
This gives
$$
p_\parallel
= \lVert p_\parallel \rVert s \frac{q}{\lVert q \rVert}
= \frac{p \cdot q}{\lVert q \rVert} \frac{q}{\lVert q \rVert}
= \frac{p \cdot q}{q^2} q
$$
and finally
$$
p_\perp
= p - p_\parallel
= p - \frac{p \cdot q}{q^2} q
$$
Here is a rendering of the scene:
The vector above the plane $z=0$ is $q$, the long vector below the plane is $p$. We see the decomposition into $p_\parallel$ (green arrows) and $p_\perp$ (red arrows). We notice that $p_\parallel$ points into the opposite direction of $q$, thus we have $s = -1$.
Find out the projection of $\bf p$ onto $\bf q$ and then subtract that from $\bf p$ to get the component perpendicular to $\bf q$. You will get the following expression $\textbf{a}=\textbf{p}-\frac{<\textbf{p,q}>}{||\textbf{q}||^2}\textbf{q}$
We perform a decomposition $$ p = p_\parallel + p_\perp $$ where $p_\parallel$ is parallel to $q$ and $p_\perp$ is perpendicular to $q$.
Then $$ p \cdot q = (p_\parallel + p_\perp) \cdot q = p_\parallel \cdot q + \underbrace{p_\perp \cdot q}_0 = p_\parallel \cdot q $$ and $$ p \cdot q = p_\parallel \cdot q = \lVert p_\parallel \rVert \lVert q \rVert s $$ with $s = \cos \angle(p_\parallel,q) \in \{-1, 1\}$ so we have $$ \lVert p_\parallel \rVert s = \frac{p \cdot q}{\lVert q \rVert} $$ This gives $$ p_\parallel = \lVert p_\parallel \rVert s \frac{q}{\lVert q \rVert} = \frac{p \cdot q}{\lVert q \rVert} \frac{q}{\lVert q \rVert} = \frac{p \cdot q}{q^2} q $$ and finally $$ p_\perp = p - p_\parallel = p - \frac{p \cdot q}{q^2} q $$
Here is a rendering of the scene:
The vector above the plane $z=0$ is $q$, the long vector below the plane is $p$. We see the decomposition into $p_\parallel$ (green arrows) and $p_\perp$ (red arrows). We notice that $p_\parallel$ points into the opposite direction of $q$, thus we have $s = -1$.
Calculation gives $$ p_\perp = (11/3, 7/3, -4/3)^\top $$