title: Find last nonzero element 3D array - numpy array
body: <p>I need to find a way to perform this operation, i have an array of shape </p>
  
  <p>[ batch_size , 150 , 1 ] </p>
  
  <p>representing batch_size sequences of integers, each sequence is 150 elements long, but in each one there are a lot of zeros that were added in order to make all the sequences of the same length. I need to find the last nonzero element for each sequence, and put it in an array, the shape of this array has to be [ batch_size ] . I'd try the following : </p>
  
  <pre><code>last = []
  for j in range(0 , inputs.shape[0] ):
    tnew = np.array( inputs[j][:][0] )
    tnew = np.trim_zeros(tnew )
     last.append( int(tnew[-1]) )
  </code></pre>
  
  <p>but I don't know if there is a better way to do this, with out having to loop over each element like that. </p>
  
  <p>Thanks for your answers and help. </p>
  
  <hr>
  
  <p>Test data</p>
  
  <pre><code>a = np.array([[[1],[0],[0],[0],[0],[0]],
                [[1],[2],[0],[0],[0],[0]],
                [[1],[2],[3],[0],[0],[0]],
                [[1],[2],[3],[4],[0],[0]],
                [[1],[2],[3],[4],[5],[0]]])
  </code></pre>