How to transpose Matrix{Any} that contains strings?

How to transpose Matrix{Any} that contains strings? The obvious way doesn’t work:

transpose(["aoeu", 2])
1×2 transpose(::Vector{Any}) with eltype Any:
Error showing value of type LinearAlgebra.Transpose{Any, Vector{Any}}:
ERROR: MethodError: no method matching transpose(::String)

Closest candidates are:
  transpose(::Missing)
   @ Base missing.jl:101
  transpose(::LinearAlgebra.Hessenberg{<:Real})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/hessenberg.jl:396
  transpose(::LinearAlgebra.Hessenberg)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/hessenberg.jl:397
  ...

Stacktrace:
  [1] getindex
    @ ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/adjtrans.jl:328 [inlined]
  [2] _getindex
    @ ./abstractarray.jl:1324 [inlined]
  [3] getindex(::LinearAlgebra.Transpose{Any, Vector{Any}}, ::Int64, ::Int64)
    @ Base ./abstractarray.jl:1291
  [4] alignment(io::IOContext{…}, X::AbstractVecOrMat, rows::Vector{…}, cols::Vector{…}, cols_if_complete::Int64, cols_otherwise::Int64, sep::Int64, ncols::Int64)
    @ Base ./arrayshow.jl:69
  [5] _print_matrix(io::IOContext{…}, X::AbstractVecOrMat, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64, rowsA::UnitRange{…}, colsA::UnitRange{…})
    @ Base ./arrayshow.jl:207
  [6] print_matrix(io::IOContext{…}, X::LinearAlgebra.Transpose{…}, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64)
    @ Base ./arrayshow.jl:171
  [7] print_matrix
    @ ./arrayshow.jl:171 [inlined]
  [8] print_array
    @ ./arrayshow.jl:358 [inlined]
  [9] show(io::IOContext{Base.TTY}, ::MIME{Symbol("text/plain")}, X::LinearAlgebra.Transpose{Any, Vector{Any}})
    @ Base ./arrayshow.jl:399
 [10] (::REPL.var"#55#56"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:273
 [11] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [12] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:259
 [13] display(d::REPL.REPLDisplay, x::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:278
 [14] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:340
 [15] #invokelatest#2
    @ ./essentials.jl:892 [inlined]
 [16] invokelatest
    @ ./essentials.jl:889 [inlined]
 [17] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:315
 [18] (::REPL.var"#57#58"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:284
 [19] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [20] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:282
 [21] (::REPL.var"#do_respond#80"{…})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:911
 [22] (::VSCodeServer.var"#101#104"{REPL.var"#do_respond#80"{…}})(mi::REPL.LineEdit.MIState, buf::IOBuffer, ok::Bool)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.75.2/scripts/packages/VSCodeServer/src/repl.jl:122
 [23] #invokelatest#2
    @ ./essentials.jl:892 [inlined]
 [24] invokelatest
    @ ./essentials.jl:889 [inlined]
 [25] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:2656
 [26] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1312
 [27] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:386
Some type information was truncated. Use `show(err)` to see complete types.

transpose is intended for use in linear algebra (ie matrices of numbers). For general matrix transposition use permutedims

1 Like

Well, this is confusing. This function should be only in LinearAlgebra, not Base, then.

Some operations such as basic operations, * / + - \ are in Base for a (not too good, but historically understandable) reason, and I suppose a few others such as ’ and transpose, and maybe none others, and everything more complex in LinearAlgebra.

permutedims works for you, really does “transpose”, but just not lazily. Which is a cool (faster) feature of transpose. I suppose transpose could be made to handle Any matrix, including strings. And it might be slightly better to have such lazy option, though doubt it needed, also I guess it was left out to catch some coding errors… since you rarely do this, at least not for linear algebra.

The parser needs to parse (1D) and 2D, and nD) arrays, so at least Julia needs to support some matrix functionality. I really wish Julia had nothing more, in Base itself, so it could exclude LinearAlgebra, for much smaller Julia since then OpenBLAS gone (LinearAlgebra.jl could be a regular package bringing it in, or not).

Strictly speaking it’s possible, I’ve done it, some operations e.g. * and / \ fail then, why it would be a breaking change, or since Julia has generic matmul, it could be done, and such slower fallback be used until you do using LinearAlgebra. I think it’s planned, just not a high priority or right away.