Declare a matrix with break lines

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:

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.


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

julia> vcat([1,2,3,
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?

1 Like

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.

This is the syntax:

a = [1 2 3; 4 5 6]
1 Like

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 :smiley:

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

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

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.

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.

1 Like

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

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.

1 Like

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:

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

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

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

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

Is that documented? I have not seen it before.

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


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

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

See also:

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?

1 Like

Nobody suggested

julia> hcat(1,2,3,
1Ă—6 Array{Int64,2}:
 1  2  3  4  5  6

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

1 Like

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.

1 Like


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 :smiley: Let’s see…