# Indexing for vector variable

**URL:** <https://discourse.pymc.io/t/indexing-for-vector-variable/17937>\
**Category:** v5\
**Created:** [September 15, 2026, 7:24pm UTC](https://discourse.pymc.io/t/indexing-for-vector-variable/17937 "2026-09-15T19:24:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSR](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@JSR](https://discourse.pymc.io/u/JSR)\
**Post date:** [September 15, 2026, 7:24pm UTC](https://discourse.pymc.io/t/indexing-for-vector-variable/17937/1 "2026-09-15T19:24:51Z")

</div>

What’s the syntax to index a vector-valued variable? The following code does what I want, but it’s rather clunky (and will only get more clunky the longer the vector).

```auto
sample_idx, sample_dims = pd.factorize(df['sample'])

coords = {'samples': sample_dims}

with pm.Model(coords=coords) as my_model:
   samples_indices = pm.Data('samples_indices', sample_idx)

   intercept_x = pm.Normal('intercept_x', mu=0, sigma=1, dims=('samples'))
   intercept_y = pm.Normal('intercept_y', mu=0, sigma=1, dims=('samples'))

   intercepts = pm.math.stack([intercept_x[samples_indices], intercept_y[samples_indices]])

```

Surely there’s a better way to do this. Something like the following?

```auto
sample_idx, sample_dims = pd.factorize(df['sample'])

coords = {'samples': sample_dims,
          'vars': ('x', 'y')}

with pm.Model(coords=coords) as my_model:
   samples_indices = pm.Data('samples_indices', sample_idx)

   intercept = pm.Normal('intercept', mu=0, sigma=1, dims=('samples', 'vars'))

   intercepts = intercept[samples_indices,:]

```

That doesn’t work, but I assume there must be some similarly elegant syntax to do this.

---

<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:** [September 16, 2026, 8:48am UTC](https://discourse.pymc.io/t/indexing-for-vector-variable/17937/2 "2026-09-16T08:48:46Z")

</div>

I’m like 99% sure the 2nd code snippet should work. What is the specific error you get?

---

<div class="post-metadata">

**Author:** ![JSR](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@JSR](https://discourse.pymc.io/u/JSR)\
**Post date:** [September 16, 2026, 12:00pm UTC](https://discourse.pymc.io/t/indexing-for-vector-variable/17937/3 "2026-09-16T12:00:11Z")

</div>

Ah, you’re right. I _was_ getting an error, but it was because I got the order of the dims wrong, and was trying to add an nxm matrix to an mxn matrix… 🤦‍♂️
