Declare a matrix with break lines


#1

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.


#2

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

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?


#3

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.


#4

This is the syntax:

a = [1 2 3; 4 5 6]

#5

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:


#6

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


#7

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.


#8

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.


#9

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

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.


#10

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 ]

#11

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]

#12

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


#13

Is that documented? I have not seen it before.


#14

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


#15

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]

#16

See also:


#17

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?


#18

Nobody suggested

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.


#19

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.


#20

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