# What is the most convenient way to create a matrix with given elements?

**URL:** <https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953>\
**Category:** Uncategorized\
**Created:** [September 29, 2021, 3:09am UTC](https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953 "2021-09-29T03:09:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [September 29, 2021, 3:09am UTC](https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953/1 "2021-09-29T03:09:04Z")

</div>

Dear all,

This must be a very dumb question.  
When I read a matlab code, just say, a simple 2 by 2 matrix A, it can be written as

```
A = [1 2; 3 4] 

```

Now in Fortran, do I have to things like below?

```
reshape( [1, 3, 2, 4], [2,2] )

```

I know I can always use reshape, but, is there more direct and convenient way to construct a Matrix?

I am asking this because in the new Fortran standard, we can already construct 1D array like

```
B = [1, 2, 3, 4, 5] 

```

It is already very similar with python/julia/matlab.  
It makes me think that perhaps modern Fortran can also construct 2D matrix like python/julia/matlab?

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [September 29, 2021, 3:34am UTC](https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953/2 "2021-09-29T03:34:35Z")

</div>

I think `reshape` is the only intrinsic possibility. But it is not too difficult to use with a bit of elegant writing, like,

```fortran
integer(IK) , parameter :: nd = 3_IK, np = 5_IK
real(RK) , parameter :: Sample(nd,np) = reshape([ 0.706046088019609031832846377421_RK, 0.031832846377421276922984960890_RK, 0.276922984960890706046088019609_RK &
                                                        , 0.046171390631154097131781235848_RK, 0.097131781235848823457828327293_RK, 0.823457828327293046171390631154_RK &
                                                        , 0.694828622975817317099480060861_RK, 0.317099480060861950222048838355_RK, 0.950222048838355694828622975817_RK &
                                                        , 0.034446080502909438744359656398_RK, 0.438744359656398381558457093008_RK, 0.381558457093008034446080502909_RK &
                                                        , 0.765516788149002795199901137063_RK, 0.795199901137063186872604554379_RK, 0.186872604554379765516788149002_RK &
                                                        ], shape = shape(Sample))

```

Just pay attention to the column-wise ordering of the elements when reshape occurs. If you like the lines of the code to truly represent the rows of the matrix, you can simply apply the `transpose` intrinsic to the reshaped matrix,

```fortran
! A lower triangle matrix.
real(RK) , parameter :: MatLow(nd,nd) = transpose(reshape( [ 1.00_RK, 0._RK, 0._RK &
                                                                , 0.25_RK, +1.391941090707505_RK, 0._RK &
                                                                , 1.00_RK, -0.538815906080325_RK, 1.307546335452338_RK ], shape = shape(MatLow)))

```

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [September 29, 2021, 4:34am UTC](https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953/3 "2021-09-29T04:34:29Z")

</div>

Unfortunately I think reshape is the only way as @shahmoradi said. However, I wish Fortran allowed the Matlab style matrix input. See this proposal:

- [Matrix literals · Issue #102 · j3-fortran/fortran\_proposals · GitHub](https://github.com/j3-fortran/fortran_proposals/issues/102)

---

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [September 29, 2021, 4:50am UTC](https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953/4 "2021-09-29T04:50:59Z")

</div>

Thank you very much @shahmoradi@certik !  
Great solutions!

Perhaps, besides reshape trick, a way kind of like matlab is to input the matrix row by row, like below,

```
A(1,:) = [1,2] 
A(2,:) = [3,4]

```

By the way, I suddenly remember somewhere perhaps in Dr. Fortran’s @sblionel youtube video,

[![](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/8/8d5c1ae7787f444e4da1600e1a5e075558a4eae4.jpeg "EU Regional School 2021 Part 1 with Steve Lionel") ](https://www.youtube.com/watch?v=lLbmb6U4cYg)

I realized that the symbol ; can be used in Fortran, so, in one line, it can be,

```
A(1,:) = [1,2]; A(2,:) = [3,4]

```

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [September 29, 2021, 5:36am UTC](https://fortran-lang.discourse.group/t/what-is-the-most-convenient-way-to-create-a-matrix-with-given-elements/1953/5 "2021-09-29T05:36:34Z")

</div>

This Matlab syntax is awful. It’s kind of convenient, but it shows all the worst parts about how matlab treats everything as a matrix and has really weird semantics around assignment. @certik while you’re looking at matrix literals, it might be a good idea to consider n dimensional array literals. Julia added these as of 1.6, and they are pretty nice. We use the rule of n semi-colons indicates a new “row” in the nth dimension. For example

```auto
A = [1 2 3
        4 5 6
       ;;;
        7 8 9
        0 1 2]

```

produces a 2x3x2 array. This is nice for people working in higher dimensions (largely physicists).
