# Matrix indexing

**URL:** <https://discourse.julialang.org/t/matrix-indexing/70057>\
**Category:** General Usage\
**Tags:** question, matrices\
**Created:** [October 19, 2021, 6:01pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057 "2021-10-19T18:01:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 19, 2021, 6:01pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057/1 "2021-10-19T18:01:24Z")

</div>

I have a matrix A with the size of m x n.

How do I get a matrix B with all the columns of A except the columns of n1, n2, and n3?

Many thanks.

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 19, 2021, 6:13pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057/2 "2021-10-19T18:13:23Z")

</div>

```julia
A[:,4:end]

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 19, 2021, 6:14pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057/3 "2021-10-19T18:14:55Z")

</div>

Thanks for the reply. I forgot to mention that n1, n2, and n3 are not consecutive. For example, I currently have 35 columns, and my n1, n2 and n3 are 21, 29 and 3 respectively.

A[:, .![21, 29, 3] ] gives me errors

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 19, 2021, 6:19pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057/4 "2021-10-19T18:19:28Z")

</div>

Yeah, what you want is the vector that has all the indices except those three. You can use the pseudo-Matlabism of `A[:, setdiff(begin:end, [n1, n2, n3])]` or you can use `Not` from [InvertedIndices.jl](https://github.com/JuliaData/InvertedIndices.jl).

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 19, 2021, 6:25pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057/5 "2021-10-19T18:25:32Z")

</div>

alternatively something like this

```julia
A[:,[!(i ∈[n1, n2, n3]) for i in 1:end]]

```

edit: but after benchmarking this it becomes clear that @mbauman is way faster

---

<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 19, 2021, 6:36pm UTC](https://discourse.julialang.org/t/matrix-indexing/70057/6 "2021-10-19T18:36:35Z")

</div>

Suggestion:

```julia
x = 1:7
m = x .* x'
7×7 Matrix{Int64}:
 1 2 3 4 5 6 7
 2 4 6 8 10 12 14
 3 6 9 12 15 18 21
 4 8 12 16 20 24 28
 5 10 15 20 25 30 35
 6 12 18 24 30 36 42
 7 14 21 28 35 42 49

m[:, 1:end .∉ Ref((2,4,6))]

 1 3 5 7
 2 6 10 14
 3 9 15 21
 4 12 20 28
 5 15 25 35
 6 18 30 42
 7 21 35 49

```
