# Efficiently deleting a column or row in a matrix

**URL:** https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534
**Category:** General Usage
**Created:** [June 29, 2017, 7:45am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534 "2017-06-29T07:45:07Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 29, 2017, 7:45am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/1 "2017-06-29T07:45:07Z")

</div>

What is the most efficient way to delete a column or a row in a matrix without reallocating the whole matrix?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 29, 2017, 7:55am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/2 "2017-06-29T07:55:04Z")

</div>

You could perhaps keep elements in a vector, relocate them when deleting, `resize!`, and `reshape` into a matrix. But I doubt that the speed gain, if any, is worth the complication.

I you are resizing repeatedly (in a loop) and allocation is a bottleneck, pre-allocate a buffer matrix for the result.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 29, 2017, 7:58am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/3 "2017-06-29T07:58:49Z")

</div>

I see so there is no straightforward way to do it.

Apparently, this comes close but the array of indices is still allocated, and there seems to be no way to index on tuple generators instead, or is there?

```julia
a = @view a[[collect(1:i-1); collect(i+1:end)], :]

```

Also the result here is a SubArray not an Array which may cause type assertion problems in my use case. And it seems that converting it to Array comes with reallocation cost.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [June 29, 2017, 8:13am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/4 "2017-06-29T08:13:22Z")

</div>

> [@mohamed82008](#):
>
> Also the result here is a SubArray not an Array which may cause type assertion problems in my use case.

Maybe you should program against `AbstractArray` and not `Array`.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 29, 2017, 8:22am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/5 "2017-06-29T08:22:43Z")

</div>

Right I thought about it, but my array shows up in:

```julia
type T1
    a::Dict{String, Array}
end

```

and changing to Dict{String, AbstractArray} will cause dispatch problems when calling `new` inside a constructor because obviously Dict{String, Array}() is not a Dict{String, AbstractArray}.

If I use a type parameter T as follows, I will be committing to a certain concrete type when the object is made, and changing it later may give an error or attempt to call convert which brings us back to allocations.

```julia
type T1{T<:AbstractArray}
    a::Dict{String, T}
end

```

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [June 29, 2017, 8:24am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/6 "2017-06-29T08:24:54Z")

</div>

Create it as a properly-typed SubArray at the outset, just don’t leave out any rows/columns?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 29, 2017, 8:30am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/7 "2017-06-29T08:30:28Z")

</div>

Right, that will work but only until it doesn’t! Let’s say I need to get the array at any point to pass it to a PyCall function for example, even for the following simple case, allocations will grow beyond reason.

```julia
julia> a = rand(5000,5000);

julia> b = @view a[:,:];

julia> @time Array(b);
  0.428287 seconds (6 allocations: 190.735 MB, 52.11% gc time)

```

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [June 29, 2017, 8:35am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/8 "2017-06-29T08:35:18Z")

</div>

Throwing PyCall into the mix is rather a large expansion of your request here; if you stick with pure Julia code we have a good strategy in place, but making it always possible to call Python (which doesn’t support most Julia types) without allocating memory is beyond the scope of stuff that you should expect to Just Work.

Perhaps try the strategy advocated in the first reply you got.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 29, 2017, 8:46am UTC](https://discourse.julialang.org/t/efficiently-deleting-a-column-or-row-in-a-matrix/4534/9 "2017-06-29T08:46:34Z")

</div>

Right, I understand that I am asking for too much, kind of a side effect of being spoiled by Julia! But perhaps another way is to replace Dict{String, Array} with Vector{Tuple{String, AbstractArray}}, and changing the rest of the code accordingly.

```julia
a = rand(5,5);
b = @view a[:,:];
c = Tuple{String, AbstractArray}[("v1", a), ("v2", b)];

```

That way if a matrix was never sliced it won’t have to be reallocated when passing it as an array.
