bob tnur | 18 Jun 2012 17:55
Picon

convert any non square matrix in to square matrix using numpy

Hi,
how I can convert (by adding zero) of any non-square numpy matrix in to square matrix using numpy? then how to find the minimum number in each row except the zeros added(for making square matrix)? ;)
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Tony Yu | 18 Jun 2012 19:04
Picon
Gravatar

Re: convert any non square matrix in to square matrix using numpy



On Mon, Jun 18, 2012 at 11:55 AM, bob tnur <bobtnur78 <at> gmail.com> wrote:
Hi,
how I can convert (by adding zero) of any non-square numpy matrix in to square matrix using numpy? then how to find the minimum number in each row except the zeros added(for making square matrix)? ;)

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Hi Bob,
 
I'm not quite sure what you're looking for (esp. the second question), but maybe something like the following?

#~~~ example code
import numpy as np

nonsquare = np.random.random(size=(3, 5))

M, N = nonsquare.shape
width = max(M, N)
square = np.zeros((width, width))
square[:M, :N] = nonsquare

min_rows = np.min(nonsquare, axis=1)
#~~~ 

-Tony
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
eat | 18 Jun 2012 19:22
Picon

Re: convert any non square matrix in to square matrix using numpy

Hi,

On Mon, Jun 18, 2012 at 6:55 PM, bob tnur <bobtnur78 <at> gmail.com> wrote:
Hi,
how I can convert (by adding zero) of any non-square numpy matrix in to square matrix using numpy? then how to find the minimum number in each row except the zeros added(for making square matrix)? ;)
Perhaps something like this:
In []: def make_square(A):
   ..:     s= A.shape
   ..:     if s[0]< s[1]:
   ....:         return r_[A, zeros((s[1]- s[0], s[1]), dtype= A.dtype)]
   ..:     return c_[A, zeros((s[0], s[0]- s[1]), dtype= A.dtype)]
   ..: 
In []: A= rand(4, 2)
In []: make_square(A)
Out[]: 
array([[ 0.76109774,  0.42980812,  0.        ,  0.        ],
       [ 0.11810978,  0.59622975,  0.        ,  0.        ],
       [ 0.54991376,  0.29315485,  0.        ,  0.        ],
       [ 0.78182313,  0.3828001 ,  0.        ,  0.        ]])
In []: make_square(A.T)
Out[]: 
array([[ 0.76109774,  0.11810978,  0.54991376,  0.78182313],
       [ 0.42980812,  0.59622975,  0.29315485,  0.3828001 ],
       [ 0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ]])

will help you.

My 2 cents,
-eat 

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Gmane