# Using PyTensor.scan to loop over multidimensional array

**URL:** <https://discourse.pymc.io/t/using-pytensor-scan-to-loop-over-multidimensional-array/11976>\
**Category:** v5\
**Created:** [April 25, 2023, 5:48pm UTC](https://discourse.pymc.io/t/using-pytensor-scan-to-loop-over-multidimensional-array/11976 "2023-04-25T17:48:39Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [April 25, 2023, 7:49pm UTC](https://discourse.pymc.io/t/using-pytensor-scan-to-loop-over-multidimensional-array/11976/4 "2023-04-25T19:49:33Z")

</div>

Scan always loops over the first dimension of stuff passed to the `sequences` argument, so you could permute the axes to put the “batch” dimension first. Another strategy would be to give a `pt.arange` as the sequence argument and the tensors as `non_sequences`, then index the tensors in the inner function. [Here’s an example](https://discourse.pymc.io/t/using-aesara-scan-to-double-loop-a-vector-over-an-matrix/10305/3) of the 2nd strategy, [here’s an example](https://discourse.pymc.io/t/issue-passing-multiple-sequences-to-aesara-scan/10709) of the 1st.

---

_[View the full topic](https://discourse.pymc.io/t/using-pytensor-scan-to-loop-over-multidimensional-array/11976)._
