#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Something about matrices


    Hi, i read a lot about slicing, indexing and arrays, but nothing seems to work in this simple case, that with matlab i resolve rapidly.

    I have n matrices, of the same dimension [h : m].
    I want to make a cicle from i=0 to n, so that i can take one matrix, extract its first column and put this one in another matrix that i call X, that now is still empty.

    So, in the first iteration I take the first column of the first matrix and put it in the first column of X-matrix, the second iteration takes the first column of second matrix and puts it in the second column of X-matrix, and so on.

    It seems not so difficult, but i tried different ways, but or it doesn't recognize iterative index i as appropriate, or it creates only a row from my columns, or the empty matrix X is bad declared... each time a different way and each time a different problem

    so, how can i do?

    thanks again
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    import scipy
    a=[[0,1],[2,3]]  # rows are 0 1  and  2 3
    b=[[4,5],[6,7]]
    
    def transpose(A):
        return list(zip(*A))
    
    input_list = [transpose(A) for A in [a,b]]
    
    cT = [A[i] for (i,A) in enumerate(input_list)]
    
    c = transpose(cT)
    
    print(c)
    Consider instead installing scipy or numpy from the enthought python distribution.

    Code:
    >>> a = scipy.arange(4).reshape((2,2))
    >>> b = a+4   # array operations similar to matlab
    array([[0, 1],
           [2, 3]])
    >>> b
    array([[4, 5],
           [6, 7]])
    >>> a[:,0:1]  # index a column vector similar to matlab
    array([[0],
           [2]])
    >>> scipy.concatenate
    <built-in function concatenate>
    >>> a.transpose()
    array([[0, 2],
           [1, 3]])
    >>> # I don't recall how to stitch columns with scipy.
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    well, it seems to be really difficult compared to matlab...

    anyway, I studied better tutorials of numpy about arrays, and with your help I made up many interesting things. not bad... thanks again... I'll be back soon to disturb with topics about plotting
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    For what it's worth, I reviewed (link) a book on numpy.


    I program in j. www.jsoftware.com
    Arbitrary rank arrays are the only data structure.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo