Numpy Matrix extension such as below:
Question: My question interesed in matrix manuplations in numpy;

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

In this matrix the biggest value is 4 as you see. I want obtain a matrix as
below, this matrix have 4 column and 10 rows (10x4) because I have 10
observation

    
    
    B=([[0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
        [0, 1, 0, 0],
        [1, 0, 0, 0],
        [0, 0, 1, 0],
        [0, 1, 0, 1],
        [1, 0, 0, 0],
        [0, 0, 1, 0],
        [0, 1, 0, 0]])
    

first row, **second** column of B matrix is should be 1 and others row element
should be 0, because first element of A matrix is two. Similarly second row
and third column shold be 1 and other row elements should be because secon
element of A matrix is 3 and so on...

How can be writen **Python**(numpy) code which give us this B matrix as output
it is very very important for me please HELP....


Answer: It looks like you just want to match `A` with the list (or array) `[1,2,3,4]`,
and mark the appropriate column

    
    
    In [110]: A=np.array([2, 3, 4, 2, 1, 3, 4, 1, 3, 2 ])
    

Use broadcasting to make a 2d true/false array of matches

    
    
    In [111]: (A[:,None]==np.arange(1,5))
    Out[111]: 
    array([[False,  True, False, False],
           [False, False,  True, False],
           [False, False, False,  True],
           [False,  True, False, False],
           [ True, False, False, False],
           [False, False,  True, False],
           [False, False, False,  True],
           [ True, False, False, False],
           [False, False,  True, False],
           [False,  True, False, False]], dtype=bool)
    

convert the T/F to 1/0 integers:

    
    
    In [112]: (A[:,None]==np.arange(1,5)).astype(int)
    Out[112]: 
    array([[0, 1, 0, 0],
           [0, 0, 1, 0],
           [0, 0, 0, 1],
           [0, 1, 0, 0],
           [1, 0, 0, 0],
           [0, 0, 1, 0],
           [0, 0, 0, 1],
           [1, 0, 0, 0],
           [0, 0, 1, 0],
           [0, 1, 0, 0]])
    

