# How to subselect columns of a matrix?

Given a matrix $A \in \mathbb{R}^{m \times n}$, I would like to obtain a column sub matrix of $A$, given a particular column index set, $\mathbb{i}$. Let us call this sub matrix, $\tilde{A}$. Is there any matrix operation I can carry out on $A$ to get $\tilde{A}$?

For example, let $$A=\left[\begin{array}{ccccc} 0.2 & 0.5 & 0.2 & -0.1 & 0.9\\ -0.5 & 0.6 & 0.3 & 0.7 & 0.8\\ 0.3 & 0.1 & -0.5 & -0.3 & 0.1 \end{array}\right],\; and \; \mathbf{i}=\left[1,3,4\right].$$ Then we have

$$\tilde{A}=\left[\begin{array}{ccc} 0.2 & 0.2 & -0.1\\ -0.5 & 0.3 & 0.7\\ 0.3 & -0.5 & -0.3 \end{array}\right].$$ I need this operation to be generalizable to all $m \times n$ matrices $A$ for all $\mathbb{i}$.

## 1 Answer

$$\tilde{A}=\begin{bmatrix} 0.2 & 0.5 & 0.2 & -0.1 & 0.9\\ -0.5 & 0.6 & 0.3 & 0.7 & 0.8\\ 0.3 & 0.1 & -0.5 & -0.3 & 0.1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0\end{bmatrix}.$$ In general, if you want to pick out $l$ columns, you’ll multiply by an $n\times l$ matrix $M$ with each column consisting of a $1$ in the row corresponding to the column that you want to pick out and zeros everywhere else. I.e., if you want column $j$ of the input to end up as column $k$ in the output, then $m_{jk}=1$ and the rest of the entries in the $k$th column are $0$. You can similarly left-multiply by a sparse matrix of $0$s and $1$s to pick out and/or permute rows.

• A simpler explanation: post-multiplying a matrix by the $j$th column of the identity matrix picks out its $j$th column; pre-multiplying by the $i$th row of the identity picks out the $i$th row.
– amd
Nov 6, 2015 at 9:25
• Thanks for this @amd, really appreciate it. Now, assume that my sub columns are given by a binary vector $\mathbb{j} = [1 0 1 1 0]$. This corresponds to $\mathbb{i} = [1,3,4]$. Is there any operation I can carry out on $\mathbb{j}$ to get the same result--i.e., recover $\tilde{A}$ just with $A$ and $\mathbb{j}$? Nov 6, 2015 at 22:34
• oops, sorry. Just realized its not possible given the dimensions of the matrices. Nov 6, 2015 at 22:43
• I suspect not, at least not with algebraic operations.. There might be a way to spread $j$ out along the diagonal of a square matrix, but I can't off the top of my head think of a way to squeeze out the zero columns. If you allow boolean operations, you can expand $j$ into a diagonal matrix with $j^Tj \operatorname{AND} I$, for instance, but you could just as well AND $j$ into each of the rows of $I$ individually if you're going that route. If I come up with anything I'll post here.
– amd
Nov 7, 2015 at 0:43
• thanks @amd, i am grateful for your help. Nov 8, 2015 at 1:17