[Numpy-discussion] another matrix operation
josef.pktd at gmail.com
josef.pktd at gmail.com
Mon Mar 15 10:23:24 EDT 2010
On Mon, Mar 15, 2010 at 9:14 AM, <josef.pktd at gmail.com> wrote:
> On Mon, Mar 15, 2010 at 9:09 AM, Gerardo Berbeglia <gberbeglia at gmail.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
>> 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)?
>
> 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):]
>>> B
array([[ 1., 0., 2., 2.],
[ 0., 1., 3., 3.],
[ 0., 0., 4., 4.],
[ 0., 0., 5., 5.]])
Josef
>
> Josef
>
>>
>> Thanks in advance.
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
More information about the NumPy-Discussion
mailing list