<s>[INST] Permutation across matrices. Matrices may be used to permute the order of elements in a set. For example:
$$
    \begin{bmatrix}
        0 & 0 & 0 & 1 \\
        1 & 0 & 0 & 0 \\
        0 & 1 & 0 & 0 \\
        0 & 0 & 1 & 0
    \end{bmatrix}
    \times
    \begin{bmatrix}
        x \\
        y \\
        z \\
        w
    \end{bmatrix}
    =
    \begin{bmatrix}
        w \\
        x \\
        y \\
        z
    \end{bmatrix}
$$
My problem is that I need to permute values in different matrices:
$$
    A = \begin{bmatrix}
        0 & 0 & x & 0 & 0 \\
        x & x & x & x & x \\
        x & 0 & 0 & 0 & x \\
        0 & 0 & 0 & 0 & 0
    \end{bmatrix}
$$
$$
    B = \begin{bmatrix}
        0 & 0 & 0 & 0 & 0 \\
        0 & 0 & y & 0 & 0 \\
        y & y & y & y & y \\
        y & 0 & 0 & 0 & y
    \end{bmatrix}    
$$
$$
    C = \begin{bmatrix}
        z & 0 & 0 & 0 & z \\
        0 & 0 & 0 & 0 & 0 \\
        0 & 0 & z & 0 & 0 \\
        z & z & z & z & z
    \end{bmatrix}    
$$
$$
    D = \begin{bmatrix}
        w & w & w & w & w \\
        w & 0 & 0 & 0 & w \\
        0 & 0 & 0 & 0 & 0 \\
        0 & 0 & w & 0 & 0
    \end{bmatrix}    
$$
Thus given the permutation matrix (from above) and the following pattern matrix how would you change all the $x$ in $A$ to $w$, all the $y$ in $B$ to $x$, all the $z$ in $C$ to $y$, and all the $w$ in $D$ to $z$:
$$
    \begin{bmatrix}
        0 & 0 & 1 & 0 & 0 \\
        1 & 1 & 1 & 1 & 1 \\
        1 & 0 & 0 & 0 & 1 \\
        0 & 0 & 0 & 0 & 0
    \end{bmatrix}
$$
 [/INST] When you multiply by a permutation matrix on the left, you permute rows (those become single entries in a column vector). When you multiply by a permutation matrix on the right, you permute columns. Here you need to permute rows.
</s>