# [Numpy-discussion] more complex matrix operation

Skipper Seabold jsseabold at gmail.com
Mon Mar 15 11:04:57 EDT 2010

```On Mon, Mar 15, 2010 at 10:58 AM, Gerardo Berbeglia
<gberbeglia at gmail.com> wrote:
> I have another matrix operations which seems a little more complicated.
>
> Let A be an n x n matrix and let S be a subset of {0,...,n-1}. Assume
> S is represented by a binary vector s, with a 1 at the index i if i is
> in S. (e.g. if S={0,3} then s = [1,0,0,1])
>
> I would like to have an efficient way to compute the function B = f
> (A,S) characterized as follows:
>
> - For each column i such that i is in S, then the column i of B is
> equal to the column i of A.
>
> - For each column i such that i is NOT in S, then the column i of B is
> equal to the ith column of the n x n identity matrix.
>
> Example. n=4.
> A = [[2,2,2,2],[3,3,3,3][4,4,4,4][5,5,5,5]]
> S = {0,2} => s=[1,0,1,0]
>
> f(A,S) = [[2,2,2,2],[0,1,0,0],[4,4,4,4],[0,0,0,1]]
>
> Which is the best way to compute f?
>