[Numpy-discussion] another matrix operation

Gerardo Berbeglia gberbeglia at gmail.com
Mon Mar 15 10:09:02 EDT 2010


I would like to do with numpy the following operation.

Let A be an n x n matrix and let s be an integer between 1 and n.

I would like to have an n x n matrix B = f(A,s) such that

- If we only look at the first s columns of B, we will not see any
difference with respect to the first s columns of the n x n identity
matrix.

- For the last n-s columns of B, we will not see any difference with
respect to the last n-s columns of A.

Example. n = 4, s = 2

A= [[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5]]

B= f(A,s) = [[1,0,2,2],[0,1,3,3],[0,0,4,4],[0,0,5,5]]

Is there a way to do this efficiently (without loops)?

Thanks in advance.



More information about the NumPy-Discussion mailing list