Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a multi-dimensional array in Julia

In MATLAB, the following syntax can be used to create 1-d matrix a and 2-d matrix b:

a = [2,3]
b = [2,3;4,5]

In Julia, constructing the 1-d array a using the same syntax works. However, constructing the 2-d array b using the same syntax fails.

Defining b as follows works:

b = cat(2,[2,4],[3,5])

Is there a syntactical shortcut for explicitly defining 2-d arrays in Julia? If so, what is it?

like image 262
Ben Hamner Avatar asked Dec 21 '22 11:12

Ben Hamner


1 Answers

You're likely looking for this:

a = [2,3]
b = [2 3;4 5]

Here's the relevant paragraph from the "Major Differences From MATLAB" section of the Julia docs:

Concatenating scalars and arrays with the syntax [x,y,z] concatenates in the first dimension (“vertically”). For the second dimension (“horizontally”), use spaces as in [x y z]. To construct block matrices (concatenating in the first two dimensions), the syntax [a b; c d] is used to avoid confusion.

like image 102
waldyrious Avatar answered Dec 26 '22 18:12

waldyrious