# How to delete following columns from matrix in julia (specific case)

**URL:** <https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607>\
**Category:** General Usage\
**Tags:** matrices\
**Created:** [October 12, 2021, 7:31am UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607 "2021-10-12T07:31:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [October 12, 2021, 7:31am UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/1 "2021-10-12T07:31:12Z")

</div>

consider following matrix,

```julia
julia> b = [-1 1 0 0 0 0;1 -1 -1 1 0 0;0 0 1 -1 -1 1;0 0 0 0 1 -1]
4×6 Matrix{Int64}:
 -1 1 0 0 0 0
  1 -1 -1 1 0 0
  0 0 1 -1 -1 1 
  0 0 0 0 1 -1

```

how do i obtain following matrix from the `b` matrix

```julia

julia> bnew 
4×3 Matrix{Int64}:
 -1 0 0
  1 -1 0
  0 1 -1
  0 0 1

```

Basically, if `[-1,1 ,0 ,0]` is the column, then `[1,-1,0,0]` is called its reflection (and vice versa). What I want is… `reflection`, if at all present in the original matrix b to be deleted from the matrix while retaining the original column vector. Can we generalize this?

another example, if

```julia
julia> B=[-1 1 0 0;1 -1 -1 0;0 0 0 1]
3×4 Matrix{Int64}:
 -1 1 0 0
  1 -1 -1 0
  0 0 0 1

```

then output should be

```julia
julia> b=[-1 0 0;1 -1 0; 0 0 1]
3×3 Matrix{Int64}:
 -1 0 0
  1 -1 0
  0 0 1

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 12, 2021, 11:44am UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/2 "2021-10-12T11:44:56Z")

</div>

Try this:

```julia
@views function remove_reflections(b)
    n = size(b,2)
    ix_drop = falses(n)
    for i = 1:n-1
        ix = i+1:n
        ix_drop[ix] .|= (Ref(-b[:,i]) .== eachcol(b[:,ix]))
    end
    return b[:, .!ix_drop]
end

```

For the provided examples it outputs:

```julia
b = [-1 1 0 0 0 0; 1 -1 -1 1 0 0; 0 0 1 -1 -1 1; 0 0 0 0 1 -1]
julia> remove_reflections(b)
 -1 0 0
  1 -1 0
  0 1 -1
  0 0 1

b = [-1 1 0 0; 1 -1 -1 0; 0 0 0 1]
julia> remove_reflections(b)
 -1 0 0
  1 -1 0
  0 0 1

```

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [October 13, 2021, 7:24am UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/3 "2021-10-13T07:24:08Z")

</div>

@rafael.guerra  
what is the following line doing ?

```julia
 ix_drop[ix] .= (ix_drop[ix] .| (Ref(-b[:,i]) .== eachcol(b[:,ix])))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 13, 2021, 7:28am UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/4 "2021-10-13T07:28:27Z")

</div>

It book keeps in the bitvector `ix_drop` the columns that are reflections of the column being analyzed in the loop. It does _bitwise or_ not to erase (and to add) to previous entries.

_ **NB:** used now the simplified shorthand syntax_ `.|=`

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [October 13, 2021, 7:39am UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/5 "2021-10-13T07:39:33Z")

</div>

i think this works as a solution, but I will try to test more with other matrices(btw, input is incidence matrix from a directional graph, so -1,1,0 are the entries in such matrix)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 13, 2021, 12:31pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/6 "2021-10-13T12:31:07Z")

</div>

Some other options for fun 🙂

```julia
@views b[:, [i==1 || b[:,i] != -b[:,i-1] for i in 1:size(b,2)]]

@views b[:, [true; eachcol(b[:, 2:end]) .!= .-eachcol(b[:, 1:end-1])]]

@views b[:, [true
             map((c,prev) -> c != -prev, eachcol(b[:,2:end]), eachcol(b))]]

@views foldl(eachcol(b[:,2:end]), init=b[:,1]) do M, c
    c == -M[:,end] ? M : [M c]
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 13, 2021, 12:57pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/7 "2021-10-13T12:57:28Z")

</div>

@sijo, it’s amazing how much code you’ve managed to put into the matrix index. And it’s pretty damn fast!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 13, 2021, 1:09pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/8 "2021-10-13T13:09:25Z")

</div>

> [@rafael.guerra](#):
>
> it’s amazing how much code you’ve managed to put into the matrix index

yes uhm not sure if it’s great for readability but it’s fun 🙂

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [October 13, 2021, 1:12pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/9 "2021-10-13T13:12:16Z")

</div>

@rafael.guerra @sijo  
How did you learn Julia to begin with? I am surprised to little new syntax everytime I come here with some query

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 13, 2021, 1:16pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/10 "2021-10-13T13:16:41Z")

</div>

> [@yewalenikhil65](#):
>
> How did you learn Julia to begin with?

I think mostly by spending time here while procrastinating… But initially I put some time in reading the manual A-Z and experimenting with the concepts in each page.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 13, 2021, 1:42pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/11 "2021-10-13T13:42:06Z")

</div>

I’m not in the best position to give advice because I’m really a beginner, but as a general rule, a good way to learn is to try to do small, useful personal projects, reading the manual and checking the forums for solutions and asking for help when you get stuck.

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [October 13, 2021, 3:46pm UTC](https://discourse.julialang.org/t/how-to-delete-following-columns-from-matrix-in-julia-specific-case/69607/12 "2021-10-13T15:46:47Z")

</div>

@rafael.guerra @sijo  
Thanks for your kind replies. Both of your solution work for me. Thanks a lot
