# Use incrementing index in transformed parameter block

**URL:** <https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668>\
**Category:** General\
**Tags:** stanc\
**Created:** [December 3, 2017, 8:12pm UTC](https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668 "2017-12-03T20:12:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhomass1](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@Bhomass1](https://discourse.mc-stan.org/u/Bhomass1)\
**Post date:** [December 3, 2017, 8:12pm UTC](https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668/1 "2017-12-03T20:12:27Z")

</div>

How do I do the following

```
transformed parameters {
	real xi[S, S]; // fix effect
	vector<lower=0.0,upper=1>[D] beta[S,S];

	int phiIndex = 0;
	for (row in 1:S){
		for (col in 1:S){
			if (row == col){
				xi[row, col] = fi[phiIndex];
				phiIndex += 1;
			}
		}
	}
	for (row in 1:S){
		for (col in 1:S){
			if (row == col){
				for (d in 1:D){
					beta[row, col, d] = fi[phiIndex];
					phiIndex += 1;
				}
			}
		}
	}
}

```

I am using phiIndex to increment the pointer for vector fi. The compiler does allow the declaration  
`int phiIndex = 0`

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [December 3, 2017, 8:30pm UTC](https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668/2 "2017-12-03T20:30:03Z")

</div>

You have to declare `int phiIndex` inside a local block within transformed parameters.

---

<div class="post-metadata">

**Author:** ![Bhomass1](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@Bhomass1](https://discourse.mc-stan.org/u/Bhomass1)\
**Post date:** [December 3, 2017, 8:36pm UTC](https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668/3 "2017-12-03T20:36:58Z")

</div>

I put the whole thing inside another { }, now it is ok with the declaration but doesn’t take the increment statement.

```
   phiIndex += 1;

```

says PARSER EXPECTED:

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [December 3, 2017, 8:38pm UTC](https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668/4 "2017-12-03T20:38:29Z")

</div>

You have to do `phiIndex = phiIndex + 1;` with Stan versions less than 2.17.0.

---

<div class="post-metadata">

**Author:** ![Bhomass1](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@Bhomass1](https://discourse.mc-stan.org/u/Bhomass1)\
**Post date:** [December 3, 2017, 9:25pm UTC](https://discourse.mc-stan.org/t/use-incrementing-index-in-transformed-parameter-block/2668/5 "2017-12-03T21:25:22Z")

</div>

thank you, fixed!
