Reputation: 4735
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?
Upvotes: 5
Views: 4396
Reputation: 497
As of Julia 0.6 depending on which dimension you seek, you could use
# hcat
b = [[2, 3] [4, 5]]
2×2 Array{Int64,2}: [2 4; 3 5]
# vcat
c = [[2 3] ; [4 5]]
2×2 Array{Int64,2}: [2 3; 4 5]
Upvotes: 0
Reputation: 3844
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.
Upvotes: 6
Reputation: 12179
You can also say [1 2; 3 4], which gives the same result as in Matlab.
Upvotes: 5
Reputation: 4735
The following syntax works (but is not as terse as the MATLAB equivalent):
b = [[2 3],[4 5]]
Upvotes: 1