# How to fix 'itkMatrixD33' object does not support indexing (itk.BinaryMask3DMeshSource)

**URL:** https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347
**Category:** Community
**Created:** [September 20, 2022, 5:21am UTC](https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347 "2022-09-20T05:21:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![cnkhanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cnkhanh](https://discourse.itk.org/u/cnkhanh)
#### Post date: [September 20, 2022, 5:21am UTC](https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347/1 "2022-09-20T05:21:31Z")

</div>

The below is the code in C++

> typename ImageType::DirectionType imageDirection=inputImage-\>GetDirection();  
> //… mesh generated using itk::BinaryMask3DMeshSource  
> typename MeshType::Pointer mesh = meshSource-\>GetOutput();  
> typename PointsContainer::Pointer points = mesh-\>GetPoints();  
> PointsContainer::Iterator PointIterator = points-\>Begin();
> 
> while(PointIterator !=points-\>End() )  
> {  
> PointType p=(PointIterator).Value();  
> PointType p2;  
> p2.Fill(0.0);
> 
> ```
> for(unsigned int iPointDim=0; iPointDim< Dimension; iPointDim++)
> {
> for(unsigned int iDim=0; iDim<Dimension; iDim++)
> {
> p2[iPointDim] += imageDirection[iPointDim][iDim]*p[iDim];
> }
> }
> 
> ```
> 
> }

* * *

I tried to do the same thing in Python

1. I read the (.hdr) image from file, getDirection() of this image.
2. I generated a mesh using itk.BinaryMask3DMeshSource
3. The while loop (from C++), I wrote as below:

```auto
for i in range(points.Size()):
        p = points.ElementAt(i)
        PixelType = itk.F
        p2 = itk.Point[PixelType, Dimension]()
        p2.Fill(0.0)
        for iPointDim in range(0, Dimension):
            for iDim in range(0, Dimension):
                p2[iPointDim] += imageDirection_itk[iPointDim][iDim]*p[iDim];

```

==\> But I received an error (TypeError: ‘itkMatrixD33’ object does not support indexing) at line “p2[iPointDim] += imageDirection\_itk[iPointDim][iDim]\*p[iDim];”

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 20, 2022, 11:59am UTC](https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347/2 "2022-09-20T11:59:12Z")

</div>

Try `imageDirection_itk(iPointDim, iDim)` and `imageDirection_itk.GetElement(iPointDim, iDim)`. Take a look at [this post](https://discourse.itk.org/t/set-image-direction-from-numpy-array/844/11) and the rest of that thread.

---

<div class="post-metadata">

### Author: ![cnkhanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cnkhanh](https://discourse.itk.org/u/cnkhanh)
#### Post date: [September 21, 2022, 12:38am UTC](https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347/3 "2022-09-21T00:38:31Z")

</div>

Thank you. It works.  
The next thing in C++ is,

```auto
(PointIterator).Value()=p2;
PointIterator++;
// End while PointIterator

```

I though that this line “(PointIterator).Value()=p2” used to set value for the current point in while loop.  
And I try to do like this in Python by the following

```auto
points.ElementAt(i) = p2

```

And I received the error “SyntaxError: can’t assign to function call”

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 21, 2022, 1:27am UTC](https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347/4 "2022-09-21T01:27:52Z")

</div>

Have you tried `points.SetElement(i, p2)`?

@Pranjal_Sahu might be more up to date on syntax for this.

---

<div class="post-metadata">

### Author: ![cnkhanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cnkhanh](https://discourse.itk.org/u/cnkhanh)
#### Post date: [September 21, 2022, 1:53am UTC](https://discourse.itk.org/t/how-to-fix-itkmatrixd33-object-does-not-support-indexing-itk-binarymask3dmeshsource/5347/5 "2022-09-21T01:53:39Z")

</div>

> [@dzenanz](#):
>
> SetElement(i, p2)

Yes, It work. Thank you so much
