# Declare a matrix with break lines

**URL:** <https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568>\
**Category:** General Usage\
**Created:** [June 10, 2018, 10:40pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568 "2018-06-10T22:40:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 10, 2018, 10:40pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/1 "2018-06-10T22:40:53Z")

</div>

Hi guys!

There is a model in which I have to declare a very large matrix (150 columns). The problem is that I do not know how can I break a line when declaring a matrix without actually creating a new row. For example:

```julia
a = [1 2 3 4 5
     6 7 8 9 10]

```

is interpreted a 2x5 matrix. Is there a way to break a line in matrix declaration without defining a new row? I am looking for something like the `...` in MATLAB.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 10, 2018, 10:50pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/2 "2018-06-10T22:50:06Z")

</div>

You can create each row as a vector, transpose it, and vcat the result:

```julia
julia> vcat([1,2,3,
             4,5,6]',
            [7,8,9,
             10,11,12]')
2×6 Array{Int64,2}:
 1 2 3 4 5 6
 7 8 9 10 11 12

```

but…really I’d suggest just not doing this. Having a 150-column matrix included in your source code seems pretty awkward no matter how you do the line breaks. Why not just store the matrix in a real format like CSV (if you want readability) or HDF5 (if you want performance) and then load it in Julia?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 10, 2018, 11:00pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/3 "2018-06-10T23:00:31Z")

</div>

I am implementing the NRLMSISE-00 model, which had those constants hard coded into the source code. I just want to use the same thing because it will be easier when a new version is released. However, the ideia to store in CSV and read it in `init` should be fine also.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 10, 2018, 11:14pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/4 "2018-06-10T23:14:10Z")

</div>

This is the syntax:

```julia
a = [1 2 3; 4 5 6]

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 10, 2018, 11:17pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/5 "2018-06-10T23:17:32Z")

</div>

Yes, but I cannot break a line without defining a new row. Hence, I have to type 150 floats in one line, which is bad 😃

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 10, 2018, 11:24pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/6 "2018-06-10T23:24:59Z")

</div>

What you mean by breaking lines? The snippet of code above doesn’t have line breaks?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 10, 2018, 11:32pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/7 "2018-06-10T23:32:14Z")

</div>

I mean, like in Matlab, in which I can declare a 10x2 matrix by:

```nohighlight
a = [1 2 3 4 5 ...
     6 7 8 9 10;
     1 2 3 4 5 ...
     6 7 8 9 10]

```

I think in Julia we do not have something to do what `...` do.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 10, 2018, 11:36pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/8 "2018-06-10T23:36:04Z")

</div>

Oh I see your point. You want to continue the row of 10 elements on the next line of the file by breaking it into chunks of size 5. I am trying to understand the rationale, I don’t have a solution for it.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 10, 2018, 11:41pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/9 "2018-06-10T23:41:43Z")

</div>

Yes, precisely. Look at the end of this FORTRAN code:

[https://ccmc.gsfc.nasa.gov/pub/modelweb/atmospheric/msis/nrlmsise00/nrlmsise00\_sub.for](https://ccmc.gsfc.nasa.gov/pub/modelweb/atmospheric/msis/nrlmsise00/nrlmsise00_sub.for)

If I want to implemente exactly as it was implemented there, then I need to declare a matrix with 150 columns.

What I am doing now is to declare each columns and then doing a cat almost like @rdeits proposed.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 11, 2018, 12:01am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/10 "2018-06-11T00:01:29Z")

</div>

Would it be too bad to ask for a feature like this in Julia? A character to break lines in array without defining a new columns, like:

```julia
A = [1 2 3 4 5 |
     7 8 9 1 2 ]

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [June 11, 2018, 12:01am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/11 "2018-06-11T00:01:43Z")

</div>

You can do this, replace MATLAB’s `...` with an open `[` and close it the next line like this:

```julia
a = [1 2 3 4 5 [
     6] 7 8 9 10;
     1 2 3 4 5 [
     6] 7 8 9 10]

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 11, 2018, 12:02am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/12 "2018-06-11T00:02:43Z")

</div>

Wow man! It works! I would have never though about that.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [June 11, 2018, 2:17am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/13 "2018-06-11T02:17:42Z")

</div>

> [@Seif\_Shebl](#):
>
> a = [1 2 3 4 5 [6] 7 8 9 10; 1 2 3 4 5 [6] 7 8 9 10]

Is that documented? I have not seen it before.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [June 11, 2018, 3:49am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/14 "2018-06-11T03:49:18Z")

</div>

It’s a work-around, there should be a formal way for doing this like MATLAB’s `...`, I don’t know of one though.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 11, 2018, 5:59am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/15 "2018-06-11T05:59:22Z")

</div>

I guess `()` also work, the goal is just to trick the parser:

```julia
a = [1 2 3 4 5 (
     6) 7 8 9 10;
     1 2 3 4 5 (
     6) 7 8 9 10]

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [June 11, 2018, 6:03am UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/16 "2018-06-11T06:03:23Z")

</div>

> [@Seif\_Shebl](#):
>
> a = [1 2 3 4 5 [6] 7 8 9 10; 1 2 3 4 5 [6] 7 8 9 10]

See also:

> [@How to create a 2x1 Array?](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337):
>
> TL;DR, answer: to get a 2x1 Array/Matrix transpose a 1x2 matrix, for example: [2 4]' A more general solution that also works for complex numbers in Julia V0.7- would be transpose([2 4]) I wanted to test julia\> maximum([1 2; 3 4], 2) 2×1 Array{Int64,2}: 2 4 So I tried julia\> maximum([1 2; 3 4], 2) == [2;4] false I expected the value to be true, but the value is false because julia\> [2;4] 2-element Array{Int64,1}: 2 4 That is, a 2×1 Array{Int64,2} is not the same as a 2-element Arra…

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 11, 2018, 3:35pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/17 "2018-06-11T15:35:57Z")

</div>

Yes, I understood that this is just a workaround to trick the parser. Question: does any Julia developer can tell me if there is any plan to add this feature? Something like Matlab’s `...`? Should I open an issue in Github?

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [June 11, 2018, 3:42pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/18 "2018-06-11T15:42:11Z")

</div>

Nobody suggested

```julia
julia> hcat(1,2,3,
       4,5,6)
1×6 Array{Int64,2}:
 1 2 3 4 5 6

```

It is clear and doesn’t require too much typing.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 11, 2018, 3:45pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/19 "2018-06-11T15:45:45Z")

</div>

> [@Ronis\_BR](#):
>
> Should I open an issue in Github?

Some discussion can’t do any harm, but please do a thorough search in the existing ones first.

I am occasionally missing a general “please pretend a line break isn’t here” syntax, eg also with specifying long string constants.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 11, 2018, 4:32pm UTC](https://discourse.julialang.org/t/declare-a-matrix-with-break-lines/11568/20 "2018-06-11T16:32:54Z")

</div>

Done: [https://github.com/JuliaLang/julia/issues/27533](https://github.com/JuliaLang/julia/issues/27533)

I have searched before about this things, and I could not find anything. However, this can be related to my skills to describe the problem in English 😃 Let’s see…
