What's the mathematical representation of shifting of an array? Consider 5x1 matrix, for example
A = \begin{bmatrix}1\\2\\3\\4\\5\end{bmatrix}
I want to shift each element down by one position and bring the last element to the first position.
B = \begin{bmatrix}5\\1\\2\\3\\4\end{bmatrix}
How can this operation be represented as a generalised mathematical expression?
Thanks in advance SEmath!
 A: The representation of this shift is simply the $5\times 5$ matrix
$$
M = \pmatrix{0&0&0&0&1\\1&0&0&0&0\\0&1&0&0&0\\0&0&1&0&0\\0&0&0&1&0}
$$
where, if you have your array $A$ stored as a column vector, then the shifted array is equal to $B = MA$.
In general, any permutation of elements in an array can be represented by a matrix which has a single $1$ in each row and in each column, and $0$ otherwise. For this reason, such matrices are called permutation matrices.
A: This operation can be represented as multiplication by a matrix that selects the appropriate rows of the vector. Here are some general principles that you can use to construct such “selector” matrices:


*

*The rows of the matrix product $AB$ are linear combinations of the rows of $B$ with coefficients given by the corresponding row of $A$. Similarly, the columns are linear combinations of the columns of $A$, with coefficients taken from the corresponding column of $B$.

*As a result, left-multiplying a matrix $B$ by a row of the identity matrix picks out the corresponding row of $B$ and right-multiplying by a column of the identity matrix picks out the corresponding column.  
You want to shuffle the rows of the vector, so you’ll be left-multiplying by some matrix $M$. The first row of the result needs to be the last row of the vector, so the first row of $M$ is $\begin{bmatrix}0&0&0&0&1\end{bmatrix}$, which is the last row of the $5\times5$ identity matrix. Similarly, the second row of the result should be the first row of the input, so the second row of $M$ is $\begin{bmatrix}1&0&0&0&0\end{bmatrix}$. Continuing in this fashion, you will end up with $$M = \begin{bmatrix}0&0&0&0&1 \\ 1&0&0&0&0 \\ 0&1&0&0&0 \\ 0&0&1&0&0 \\ 0&0&0&1&0 \end{bmatrix}.$$ A matrix like this that produces a permutation of the rows or columns of another matrix is known as a permutation matrix, but using the principles above you can also represent other operations that select parts of matrices as matrix multiplications.
