Home > Software design >  How to delete a column in a matrix in Julia?
How to delete a column in a matrix in Julia?

Time:07-07

If I had a matrix M such that M = [0 1 2; 3 4 5; 6 7 8], how could I delete a specified column. For example, after I deleted the second column M would be [0 2; 3 5; 6 8].

In numpy, there exists a numpy.delete function that does what I ask (deleting along a specific axis), but I am unsure as to what the Julia equivalent is.

CodePudding user response:

You can do it directly with array indexing,

julia> M = [0 1 2; 3 4 5; 6 7 8];

julia> M[:, 1:3 .≠ 2]
3×2 Matrix{Int64}:
 0  2
 3  5
 6  8

note that is written as \neq Tab from keyboard.

Or using packages like InvertedIndices.jl:

julia> using InvertedIndices

julia> M[:, Not(2)]
3×2 Matrix{Int64}:
 0  2
 3  5
 6  8
  • Related