title: TensorFlow - Creating a Tensor out of a stencil
body: <p>While searching for this problem I haven´t found something related, so please let me know if something similar already exists. </p>
  
  <p>So here is the problem. Imagine you have a stencil Tensor <strong>A</strong> containing the non-zero values of a column of <strong>B</strong>. Now I want to generate a Tensor <strong>B</strong> containing the values of <strong>A</strong> at the correct position. A should be of dim (n,3) and B of dim (2n+1,n). The different dimension arise from different Finite Element Grids. For example let n=3 say we have <strong>A</strong></p>
  
  <pre><code>A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  </code></pre>
  
  <p>and want to create a Tensor <strong>B</strong> out of <strong>A</strong> like</p>
  
  <p>B[0,0]=A[0,0], B[1,0]=A[0,1], B[2,0]=A[0,2], B[2,1]=A[1,0], B[3,1]=A[1,1], B[4,1]=A[1,2], B[4,2]=A[2,0], B[5,2]=A[2,1] and B[6,2]=A[2,2].</p>
  
  <p>So for the example <strong>B</strong> would be</p>
  
  <pre><code>B = [[1, 0, 0], [2, 0, 0], [3, 4, 0], [0, 5, 0], [0, 6, 7], [0, 0, 8], [0, 0, 9]].
  </code></pre>
  
  <p>Additionaly this needs to be differentiable, if that is even possible. Since I want to train the Tensor <strong>A</strong> to some parameters, but use <strong>B</strong> to calculate a Matrix-vector product for the loss funktion. I won´t use batch learning at this point.</p>
  
  <p>I tried to use the <a href="https://www.tensorflow.org/versions/r1.14/api_docs/python/tf/assign" rel="nofollow noreferrer">tf.assign()</a> function to assign every single Value for the Tensor <strong><em>B</em></strong>. I am looking for a better option since this is getting really messy for large tensors and you have to run the tf.assign() op before the value is even set. </p>
  
  <p>In numpy I would simply do something like</p>
  
  <pre><code>import numpy as np
  A = np.array([[1,2,3],[4,5,6],[7,8,9]])
  B = np.zeros((7,3))
  for i in range(3):
      B[2*i:2*i+3,i] = A[i,:]
  </code></pre>
  
  <p>is there some similar or even easier way to do this in Tensorflow at which the operation is still differentiable?</p>