# How to get the sub-matrix of A?

**URL:** https://community.freefem.org/t/how-to-get-the-sub-matrix-of-a/23
**Category:** General Discussion
**Created:** [April 10, 2019, 2:03pm UTC](https://community.freefem.org/t/how-to-get-the-sub-matrix-of-a/23 "2019-04-10T14:03:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wys](https://avatars.discourse-cdn.com/v4/letter/w/9e8a1a/32.png) [@wys](https://community.freefem.org/u/wys)
#### Post date: [April 10, 2019, 2:03pm UTC](https://community.freefem.org/t/how-to-get-the-sub-matrix-of-a/23/1 "2019-04-10T14:03:57Z")

</div>

Dear all,

I am new to FreeFEM++ and I want to ask a simple question.  
If I have already obtained a matrix by **matrix A = va(Vh, Vh);**, how to get the sub-matrix of A? For example in Matlab, you can simply write **A(1:i, 1:j)**.

Thanks for your time.  
wys

---

<div class="post-metadata">

### Author: ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)
#### Post date: [April 10, 2019, 7:27pm UTC](https://community.freefem.org/t/how-to-get-the-sub-matrix-of-a/23/2 "2019-04-10T19:27:19Z")

</div>

I do not know if a simple technique exists for sparse matrix, but you can use the [I, J, C] decomposition (see [here](https://doc.freefem.org/references/types.html#matrix)) to manually modify your matrix

---

<div class="post-metadata">

### Author: ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)
#### Post date: [April 12, 2019, 12:06pm UTC](https://community.freefem.org/t/how-to-get-the-sub-matrix-of-a/23/3 "2019-04-12T12:06:19Z")

</div>

Update: It is possible using indexes.

A working example:

```auto
mesh Th = square(10, 10);
fespace Uh(Th, P1);
macro grad(u)[dx(u), dy(u)]//
varf vL (u, uh)
	= int2d(Th)(
		  u * uh
		+ grad(u)'*grad(uh)
	)
	+ on(1, 2, u=0)
	;
matrix L = vL(Uh, Uh);

int imin = 10;
int imax = 50;
int jmin = 15;
int jmax = 45;
int[int] I(imax-imin);
for (int i = 0; i < I.n; i++)
	I[i] = imin + i;
int[int] J(jmax-jmin);
for (int j = 0; j < J.n; j++)
	J[j] = jmin + j;

matrix L2;
L2 = L(I, J);

```

---

<div class="post-metadata">

### Author: ![wys](https://avatars.discourse-cdn.com/v4/letter/w/9e8a1a/32.png) [@wys](https://community.freefem.org/u/wys)
#### Post date: [April 13, 2019, 11:29am UTC](https://community.freefem.org/t/how-to-get-the-sub-matrix-of-a/23/4 "2019-04-13T11:29:34Z")

</div>

Thank you very much!
