[SciPy-user] unexpected matrix behavior

Travis Oliphant oliphant.travis at ieee.org
Thu Dec 15 22:13:13 CST 2005


Alan G Isaac wrote:

>I would like to understand the behavior of matrices,
>as shown below.  Is it expected?  If so, what is the
>general principle?
>
>  
>
Yes this is expected...

>>>>x = [[1,2],[3,4]]
>>>>y = scipy.array(x)
>>>>z = scipy.mat(x)
>>>>xz = zip(*x)
>>>>yz = zip(*y)
>>>>zz = zip(*z)
>>>>xz
>>>>        
>>>>
>[(1, 3), (2, 4)]
>  
>
>>>>yz
>>>>        
>>>>
>[(1, 3), (2, 4)]
>  
>
>>>>zz
>>>>        
>>>>
>[(matrix([[1, 2]]), matrix([[3, 4]]))]
>
>  
>
The general principle is that matrices are *always*  two-dimensional and 
when you slice a matrix you get a matrix. 

Thus, the essential difference is that

z[0] is another two-dimensional object while y[0] and x[0] are both 
one-dimensional objects.

Compare:  len(z[0]) with len(x[0]) and len(y[0]) to see what's really 
going on.

An array view of the same data is always available as z.A

So, zip(*z.A) gives

[(1, 3), (2, 4)]


-Travis

>
>_______________________________________________
>SciPy-user mailing list
>SciPy-user at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-user
>  
>



More information about the SciPy-user mailing list