# Re: [theano-users] How to transform the value in a matrix to its index in another vector?

```On Thu, Oct 13, 2016, Xiang Li wrote:
> Given a matrix A and a vector B, I want to transform the value in A to its
> index in B.
> e.g. A=[[1,2],[2,5]], B=[1,2,3,4,5], so the desired result is A'=[[0,1],
> [1,4]], since B[0]=1, B[1]=2, B[4]=5
> How to achieve  the function by a basic operation?```
```
In numpy, you could do:
>>> B_ = np.zeros(B.max() + 1, int)
>>> B_[B] = np.arange(B.shape[0])
>>> B_[A]
array([[0, 1],
[1, 4]])

In Theano, it is not that different:
>>> A = imatrix('A')
>>> B = ivector('B')
>>> B_ = tensor.zeros([B.max() + 1], 'int32')
>>> B_ = tensor.set_subtensor(B_[B], tensor.arange(B.shape[0]))
>>> A_ = B_[A]
>>> A_.eval({A: [[1,2],[2,5]], B: [1,2,3,4,5]})
array([[0, 1],
[1, 4]], dtype=int32)

--
Pascal

--

---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email