TITLE: Constructing two matrices that do not commute
QUESTION [9 upvotes]: I need to construct square matrices $A$ and $B$ such that $AB=0$ but $BA \neq 0$. 
I know matrix multiplication is not commutative, but I don't know how to construct such matrices. Thanks in advance.
Edit: looking for some simple way

REPLY [2 votes]: There is a quite easy general way to get an example. Saying $AB=0$ means that the image space of $B$ must be contained in the kernel of $A$, while $BA\neq0$ means the image of $A$ is not contained in the kernel of $B$. Clearly you won't get an example by choosing $A$ or $B$ to be zero or invertible, so their images and kernels have to be proper nonzero subspaces of the whole space $V=K^n$ these matrices act upon.
Now the rank-nullity theorem ties the dimensions of the kernel and image of the same matrix to each other (their sum must be$~n$), but that is the only inevitable relation. So you can start with a simple example for which $AB=0$, and if it happens that $BA=0$ too, you can modify say $B$ in such a way that you keep its image space unchanged, but modify its kernel (to not contain the image of $A$). For instance, let $A$ be a projection on some subspace $U$ of $V$ parallel to a complementary subspace $W$. To keep things simple take $U$ the span of an initial $k$ standard basis vectors and $W$ the span of the remaining $n-k$ vectors, so $A$ is diagonal with first $k$ diagonal entries equal to$~1$ and the remaining $n-k$ diagonal entries equal to$~0$. You can assure $AB=0$ by having $B$ be a projection on the subspace $W$ (which is the kernel of $A$). Now the kernel of $B$ has dimension$~k$, and we want this to not contain$~U$, image of $A$ which does have dimension$~k$ (so we just need those subspaces to differ). That excludes the most obvious choice of taking for$~B$ the projection$~P$ parallel to the complement $U$ of$~W$ (that's the diagonal matrix with entries $0$ where $A$ has$~1$ and vice versa).
There are at least two ways to fix this. One is to simply choose a different complement $U'$ of$~W$. This is easy to achieve, for instance by just changing one of the spanning (standard basis) vectors of $U$ to have a nonzero coordinate for one of the (final standard basis) vectors spanning $W$. An easier and more concrete approach is to take the (diagonal) matrix of $P$, and perform some column operation(s) on it, which will not change the image (column space), but it will in general change the kernel. For instance just permute some of the zero columns with nonzero columns, which will work fine. Quite likely the result will no longer be a projection matrix at all, so this is a more radical change than obtained by projecting parallel to a different complementary subspace.