[Numpy-discussion] another matrix operation
Mon Mar 15 09:23:24 CDT 2010
On Mon, Mar 15, 2010 at 9:14 AM, <firstname.lastname@example.org> wrote:
> On Mon, Mar 15, 2010 at 9:09 AM, Gerardo Berbeglia <email@example.com> wrote:
>> 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
>> - 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)?
> something like this ?
> B = A.copy()
> B[:s,:] = np.eye[:s,:]
that changes rows,
this saves building one temporary eye
>>> n,s = 4,2
>>> A = np.array([[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5]])
>>> B = np.eye(n)
>>> B[:,-(n-s):] = A[:,-(n-s):]
array([[ 1., 0., 2., 2.],
[ 0., 1., 3., 3.],
[ 0., 0., 4., 4.],
[ 0., 0., 5., 5.]])
>> Thanks in advance.
>> NumPy-Discussion mailing list
More information about the NumPy-Discussion