-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Corner cases of hvcat [...] syntax: column matrices #342
Comments
+1 since I tripped over this recently. It's super counterintuitive that |
IMO having easy syntax for both creating a vector and a one-column matrix would be handy. We could consider [1; 2] # gives a one-column matrix
[1; 2;] # gives a vector BUT
JuliaLang/julia#16740 could make |
Now that we have RowVectors, we could actually make |
You can create vectors with |
My current thinking is that something like this would be nice:
It's a bit too Friday afternoon for a good idea for the fourth case... |
Opening with
This would give the following syntaxes:
This creates a syntax for a zero-element hvcat since the semicolon is both leading and trailing, but leaves no zero-element syntaxes for hcat or vcat. I'm not sure how I feel about this. The rule is fairly simple, but it feels kind of visually messy. Not sure it would be an improvement. |
I think using the semicolon syntax for creating a one-column matrix is the right way to go. Appending vectors would no longer be able to use semicolon for this purpose but you could do I wrote a mini test suite just to get a feel for how things currently work and potentially could work: @test size([1 2 3]) == (1, 3)
@test size([1; 2; 3]) == (3, 1) # Currently fails
@test size([1, 2, 3]) == (3,)
@test size([1]) == (1,)
@test size([1;]) == (1, 1) # Currently fails
v = [1, 2]
@test [v v] == [1 1; 2 2]
@test [v; v] == [1 2 1 2]' # [1; 2; 1; 2], Currently fails
@test [v, v] == [[1, 2], [1, 2]]
@test [v... v...] == [1 2 1 2]
@test [v...; v...] == [1 2 1 2]' # [1; 2; 1; 2]
@test [v..., v...] == [1, 2, 1, 2]
m = [1 2]
@test [m m] == [1 2 1 2]
@test [m; m] == [1 2; 1 2]
@test [m, m] == [[1 2], [1 2]]
m = [1 2]' # [1; 2]
@test [m m] == [1 1; 2 2]
@test [m; m] == [1 2 1 2]' # [1; 2; 1; 2]
@test [m, m] == [[1 2]', [1 2]'] # [[1; 2], [1; 2]]
m = [1 2; 3 4]
@test [m m] == [1 2 1 2; 3 4 3 4]
@test [m; m] == [1 2; 3 4; 1 2; 3 4]
@test [m, m] == [[1 2; 3 4], [1 2; 3 4]] |
A semicolon creating a 2-D array in all cases except for when combining vectors or scalars has been the only un-intuitive part of Julia's array syntax for me. The syntax suggested by @omus makes a lot of sense. Spaces are always used to concatenate horizontally (2-D). Semicolons are always used to concatenate vertically (2-D). Commas are always used to concatenate one-dimensionally. If a splat is used, then it unravels the iterator before doing any of the above concatenations. This is shown well in his examples. It gives a visually-similar yet distinct way to create and display the three cases. An example that makes the current syntax even more confusing: a = [1 2] # 1×2 Array
b = hvcat(1, [1, 2]) # 2×1 Array
c = [1, 2] # 2-element Array
[a, b, c] # forces it to print all three, gives:
3-element Array{Array{Int64,N} where N,1}:
[1 2]
[1; 2]
[1, 2] First, the syntax I used to create the 2x1 Array doesn't fit with the other two. I had to do a lot of searching and doc reading to get to it. Second, the printed output of Julia itself differentiates the 2x1 Array from the other two by using a semicolon. That is confusing. This is a case where the language could be improved. |
For constructing one-column matrices, couldn't we just define a constructor: Matrix(x::Vector) = reshape(x, length(x), 1)
Matrix(x::AbstractVector) = Matrix(Vector(x)) ? These are currently a |
The latest version of JuliaLang/julia#33697 provides a syntax for this: |
Hi, Following StefanKarpniski's comment in Jan, is it time to close this issue? Matt. |
For everyone who (like me) arrives here searching to generate an nx1 matrix: |
hvcat
concatenation syntax produces matrices, such as inBut there is no corresponding way to create a single column matrix:
I suggest that the latter should invoke
hvcat
and notvcat
, to produce matrices. This implies that there would be no specialvcat
syntax any more.The text was updated successfully, but these errors were encountered: