Skip to content
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

Unexpected behaviour of cat for empty matrix entry #57438

Open
andreasvarga opened this issue Feb 17, 2025 · 2 comments
Open

Unexpected behaviour of cat for empty matrix entry #57438

andreasvarga opened this issue Feb 17, 2025 · 2 comments
Labels
arrays [a, r, r, a, y, s]

Comments

@andreasvarga
Copy link
Contributor

The following is unexpected to me:

julia> cat(Float64[], rand(2,2), dims=Val((1,2)))
2×3 Matrix{Float64}:
 0.0  0.82864   0.222715
 0.0  0.230898  0.594456

which is the same as

julia> cat(zeros(0,1), rand(2,2), dims=Val((1,2)))
2×3 Matrix{Float64}:
 0.0  0.0953531  0.692899
 0.0  0.410014   0.0780325

which is correct. Also

julia> cat(zeros(1,0), rand(2,2), dims=Val((1,2)))
3×2 Matrix{Float64}:
 0.0       0.0
 0.60077   0.539492
 0.891619  0.0582591

is as expected.

To obtain the expected result I needed to use

julia> cat(zeros(0,0), rand(2,2), dims=Val((1,2)))
2×2 Matrix{Float64}:
 0.284389  0.356856
 0.229907  0.651545

Is this behaviour the right one, or is a bug?

@dkarrasch dkarrasch added the arrays [a, r, r, a, y, s] label Feb 17, 2025
@mcabbott
Copy link
Contributor

I think this is expected, because the empty vector is effectively 0x1 (or if more dimensions matter, it is 0x1x1x1 etc.)

julia> size([], 1)
0

julia> size([], 2)
1

Concatenating an empty vector with a non-empty one arguably relies on it not being 0x0:

julia> vcat(Float64[], ones(2))
2-element Vector{Float64}:
 1.0
 1.0

julia> vcat(Float64[], ones(2,1))
2×1 Matrix{Float64}:
 1.0
 1.0

julia> vcat(zeros(0,0), ones(2))
ERROR: DimensionMismatch: number of columns of each array must match (got (0, 1))

@andreasvarga
Copy link
Contributor Author

andreasvarga commented Feb 20, 2025

Frankly: Why should have an empty array column dimension 1? In Matlab size([ ]) = (0,0).
I realize that in Julia this is a very basic issue, but

size([],2) = 0

would still be the right choice. All concatenations could accept an empty array, without any effect (similarly as in Matlab).

My final comment: Knowing the current behaviour, it is easy to programmatically overcome possible ambiguous situations.
So, this issue can be closed from my point of view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

No branches or pull requests

3 participants