[Numpy-discussion] Matrix operation.

Vicente Sole sole@esrf...
Fri Apr 2 10:49:37 CDT 2010


With A and X being arrays:

B=numpy.zeros(A.shape, A.dtype)
B[A>0] = X

Armando


Quoting gerardob <gberbeglia@gmail.com>:

>
> Let A be a square matrix of 0's and 1's, and let X be a one dimesional
> vector.
> The length of X is equal to the number of 1's that A has.
> I would like to produce a new matrix B by traversing the matrix A row by row
> and:
> 1- whenever i find a 0, set B in that position to zero.
> 2- whenever i find a 1, set B in that position with the ith value of X
> (where i represents the ith time i found a 1).
>
> Example.
> input:
> A=[[1,1,0],
> [1,0,0],
> [0,0,1]]
>
> X=[2,9,10,3]
>
> Output:
> B =[[2,9,0],
>    [10,0,0],
>    [0,0,3]]
>
> Which is an efficient way to accomplish this using numpy?
>
> Thanks.
> --
> View this message in context:   
> http://old.nabble.com/Matrix-operation.-tp28119663p28119663.html
> Sent from the Numpy-discussion mailing list archive at Nabble.com.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list