# Modify an element in a matrix

**URL:** https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398
**Category:** Engineering
**Created:** [October 8, 2022, 8:38am UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398 "2022-10-08T08:38:19Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zhuangming.shen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zhuangming.shen](https://discourse.itk.org/u/zhuangming.shen)
#### Post date: [October 8, 2022, 8:38am UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/1 "2022-10-08T08:38:19Z")

</div>

Hi, if I want to modify an element in a matrix, in ITK 4.13.3, I can code like

 ![image](https://discourse.itk.org/uploads/default/original/2X/0/0c977731346ee0d0921238a5f4ba7715dd12d09e.png)  
But in ITK 5.2.1, the same code seems fail to change the element.  
 ![image](https://discourse.itk.org/uploads/default/original/2X/a/adbf67a330a1484436ef1a7067bfe2ee6646b414.png)  
I’d like to know what’s the way to do this in ITK 5. Thanks in advance.

---

<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: [October 10, 2022, 1:51pm UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/2 "2022-10-10T13:51:07Z")

</div>

@matt.mccormick, trying the above code on my computer also gives me an error, in two different environments:

```nohighlight
Python 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:45) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import itk
>>> a=itk.Matrix[itk.D,3,3]
>>> a.GetVnlMatrix().set(0,0,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: GetVnlMatrix() missing 1 required positional argument: 'self'
>>> itk. __file__'C:\\Users\\Dzenan\\miniconda3\\envs\\deep_learning\\lib\\site-packages\\itk\\ __init__.py'
>>> itk. __version__'5.2.1'

```

```nohighlight
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import itk
>>> a=itk.Matrix[itk.D,3,3]
>>> a.GetVnlMatrix().set(0,0,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: GetVnlMatrix() missing 1 required positional argument: 'self'
>>> itk. __file__'M:\\a\\ITK-py\\Wrapping\\Generators\\Python\\itk\\ __init__.py'
>>> itk. __version__'5.3.0'

```

---

<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: [October 10, 2022, 2:04pm UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/3 "2022-10-10T14:04:10Z")

</div>

Ooops, I forgot instantiation parentheses. With that I get the same output as @zhuangming.shen in both of these enviroments.

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [October 10, 2022, 5:13pm UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/4 "2022-10-10T17:13:01Z")

</div>

I think the use of `set` was incorrect originally. `vnl_matrix` has a `set` method, but it’s not doing what you are trying to do there.

```auto
  //: Fills (laminates) this matrix with the given data, then returns it.
  // A synonym for copy_in()
  vnl_matrix& set(T const *d) { return copy_in(d); }

```

If you want to set a pixel in a `vnl_matrix`, you need to use `put` instead of `set`. Or probably easier, use `itk.GetArrayViewFromVnlMatrix`. And use `numpy` operator `[]` to manipulate it.

I am not sure if the ITK4 code is calling a constructor on-the-fly with arguments `(0, 0, 1)` and passing it to `set`?

---

<div class="post-metadata">

### Author: ![zhuangming.shen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zhuangming.shen](https://discourse.itk.org/u/zhuangming.shen)
#### Post date: [October 11, 2022, 12:38am UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/5 "2022-10-11T00:38:11Z")

</div>

I tried “put” instead of “set”, but still fail to change the element in ITK 5.2.1.

 ![image](https://discourse.itk.org/uploads/default/original/2X/c/c7a89d945e3da8c308b0f8fbe00ede766581a0b0.png)

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [October 11, 2022, 9:15am UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/6 "2022-10-11T09:15:23Z")

</div>

Umm, the problem is that the GetVnlMatrix() returns a copy, instead of a reference.

```auto
In [2]: m = itk.Matrix[itk.D, 3,3]()

In [3]: m
Out[3]: itkMatrixD33 ([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])

In [4]: m.GetVnlMatrix()
Out[4]: <itk.vnl_matrix_fixedPython.vnl_matrix_fixedD_3_3; proxy of <Swig Object of type 'vnl_matrix_fixedD_3_3 *' at 0x7fb3662bdb90> >

In [5]: m.GetVnlMatrix()
Out[5]: <itk.vnl_matrix_fixedPython.vnl_matrix_fixedD_3_3; proxy of <Swig Object of type 'vnl_matrix_fixedD_3_3 *' at 0x7fb423e87240> >

```

It was changed here:

> <https://github.com/InsightSoftwareConsortium/ITK/commit/fede4b865d2ea7a4fdde0d81fc496b7054e8cfbd>
>
> When getting a VNL matrix from an ITK matrix, a reference is returned. This
> crea…tes problem in Python if on uses the variable multiple times in one line
> (e.g. m2=m\*m).
> To work araound this problem, we define a Python function that copies the
> VNL matrix. Another solution would have been to use a SWIG \`typemap\`.

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [October 11, 2022, 9:33am UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/7 "2022-10-11T09:33:48Z")

</div>

Opened an issue here:

> <https://github.com/InsightSoftwareConsortium/ITK/issues/3683>
>
> \### Description
> 
> \`GetVnlMatrix()\` was retuning a reference before 5.0, now it …returns a copy.
> Not sure how we modify a matrix now.
> It was reported here: https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/5
> This was introduced as a bug fix here: https://github.com/InsightSoftwareConsortium/ITK/commit/fede4b865d2ea7a4fdde0d81fc496b7054e8cfbd
> 
> To avoid problems when using the vnl matrix multiple times in the same line: \`m2 = m\*m\`.
> 
> \### Steps to Reproduce
> 
> \`\`\`python
> In \[2\]: m = itk.Matrix\[itk.D, 3,3\]()
> 
> In \[3\]: m
> Out\[3\]: itkMatrixD33 (\[\[0.0, 0.0, 0.0\], \[0.0, 0.0, 0.0\], \[0.0, 0.0, 0.0\]\])
> 
> In \[4\]: m.GetVnlMatrix()
> Out\[4\]: \<itk.vnl\_matrix\_fixedPython.vnl\_matrix\_fixedD\_3\_3; proxy of \<Swig Object of type 'vnl\_matrix\_fixedD\_3\_3 \*' at 0x7fb3662bdb90\> \>
> 
> In \[5\]: m.GetVnlMatrix()
> Out\[5\]: \<itk.vnl\_matrix\_fixedPython.vnl\_matrix\_fixedD\_3\_3; proxy of \<Swig Object of type 'vnl\_matrix\_fixedD\_3\_3 \*' at 0x7fb423e87240\> \>
> 
> \`\`\`
> We can see that GetVnlMatrix returns a new pointer every time, instead of a reference to the actual vnl\_matrix in the itkMatrix.
> 
> \### Expected behavior
> 
> I would expect to get a reference.
> Also, how do we modify the matrix if we cannot access the inner vnl matrix? We don't have operators in itk.Matrix.
> 
> \### Actual behavior
> 
> GetVnlMatrix() returns a copy.
> 
> \### Reproducibility
> 
> Always
> 
> \### Versions
> 
> The change was commited to 5.0rc2. here: https://github.com/InsightSoftwareConsortium/ITK/commit/fede4b865d2ea7a4fdde0d81fc496b7054e8cfbd

Thanks for reporting it @zhuangming.shen  
I am not sure right now how to modify an element from an itk.Matrix in python.

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [October 11, 2022, 9:53am UTC](https://discourse.itk.org/t/modify-an-element-in-a-matrix/5398/8 "2022-10-11T09:53:56Z")

</div>

Since ITK can now convert matrices from / to NumPy arrays, I think the best is to do something like

```auto
>>> import itk
>>> import numpy as np
>>> m = itk.Matrix[itk.D, 3,3]()
>>> m.GetVnlMatrix().get(0,0)
0.0
>>> a=itk.array_from_matrix(m)
>>> a[0,0]=1
>>> m=itk.matrix_from_array(a)
>>> m.GetVnlMatrix().get(0,0)
1.0

```
