Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: delete rows and columns from an array or matix

How can I delete one or more rows and/or columns from an array?

like image 985
Jeffrey Sarnoff Avatar asked Sep 20 '19 18:09

Jeffrey Sarnoff


People also ask

How do you delete a row in Julia Matrix?

You can't delete a row from a matrix – the fact that Matlab has easy syntax for this is a bit of a trap because the actual way you have to delete a row is to create a copy without the row so we decided to make that explicit and thereby have more transparent performance characteristics.

How do I remove an element from an array in Julia?

Julia allows to delete an element from a 1D array by using predefined function pop!. This function will simply remove the last element of the array and reduce the array size by 1. To delete first element of the array, one can use popfirst! function.

How do you delete rows and columns in a matrix?

The easiest way to remove a row or column from a matrix is to set that row or column equal to a pair of empty square brackets [] . For example, create a 4-by-4 matrix and remove the second row. Now remove the third column.

How do you delete a row from an array?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.).


1 Answers

Working with:

julia> array = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
4×4 Array{Int64,2}:
  1   2   3   4
  5   6   7   8
  9  10  11  12
 13  14  15  16

To delete a single row (here row 2):

julia> newarray = array[1:end .!= 2, :]
3×4 Array{Int64,2}:
  1   2   3   4
  9  10  11  12
 13  14  15  16

To delete a single column (here column 3):

julia> newarray = array[:, 1:end .!= 3]
4×3 Array{Int64,2}:
  1   2   4
  5   6   8
  9  10  12
 13  14  16

To delete a single row and a single column (here row 2, column 3):

julia> newarray = array[1:end .!= 3, 1:end .!= 3]
3×3 Array{Int64,2}:
  1   2   4
  5   6   8
 13  14  16

To delete multiple rows (here rows 2, 4):

julia> newarray = array[setdiff(1:end, (2,4)), :]
2×4 Array{Int64,2}:
 1   2   3   4
 9  10  11  12

To delete multiple columns (here columns 2, 4):

julia> newarray = array[:, setdiff(1:end, (2,4))]
4×2 Array{Int64,2}:
  1   3
  5   7
  9  11
 13  15

To delete a single row and multiple columns (here row 4 and columns 3, 4):

julia> newarray = array[1:end .!= 4, setdiff(1:end, (3,4))]
3×2 Array{Int64,2}:
 1   2
 5   6
 9  10

# or

julia> newarray = array[setdiff(1:end, 4), setdiff(1:end, (3,4))]
3×2 Array{Int64,2}:
 1   2
 5   6
 9  10

# or
julia> newarray = array[setdiff(1:end, (4,)), setdiff(1:end, (3,4))]
3×2 Array{Int64,2}:
 1   2
 5   6
 9  10

To delete multiple rows and columns (here rows 1, 2 and columns 3, 4):

julia> newarray = array[setdiff(1:end, (1,2)), setdiff(1:end, (3,4))]
2×2 Array{Int64,2}:
  9  10
 13  14
like image 118
Jeffrey Sarnoff Avatar answered Oct 25 '22 01:10

Jeffrey Sarnoff