# Extract a subarray from an array by eliminating certain rows and columns

**URL:** <https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974>\
**Category:** General Usage\
**Created:** [August 7, 2018, 6:37am UTC](https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974 "2018-08-07T06:37:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norman](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@Norman](https://discourse.julialang.org/u/Norman)\
**Post date:** [August 7, 2018, 6:37am UTC](https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974/1 "2018-08-07T06:37:18Z")

</div>

I have two one-dimensional arrays, R and C. R is the row numbers and C is the column numbers. I need to get a subarray from an array A such that the new array does NOT contain certain rows and columns in A based on R and C.

To illustrate:

```
A = randn(5, 5)
R = [2, 3]
C = [4, 5]

```

I want to get an array by eliminating the 4th and 5th columns in A and also the 2nd and 3rd rows in A. What is a good way to do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![Norman](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@Norman](https://discourse.julialang.org/u/Norman)\
**Post date:** [August 7, 2018, 4:00pm UTC](https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974/2 "2018-08-07T16:00:44Z")

</div>

Ok…I just figured out a relatively easy way to do this by myself.

```
All_index = collect(1:size(A,1))
R1 = deleteat!(All_index, R)
C1 = deleteat!(All_index, C)
A = [:, C1]
A = [R1, :]

```

Not sure whether there is a more efficient or build-in method to do this kind of things.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [August 8, 2018, 11:14pm UTC](https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974/3 "2018-08-08T23:14:02Z")

</div>

I suspect the following will be more efficient for larger arrays:

```
not_in(inds::AbstractVector{Int}, n::Int)::Vector{Int} = setdiff(1:n, IntSet(inds))
A = randn(5, 5)
R = [2, 3]
C = [4, 5]
A[not_in(R, size(A,1)), not_in(C, size(A,2))]

```

---

<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:** [August 9, 2018, 12:00am UTC](https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974/4 "2018-08-09T00:00:15Z")

</div>

Check out this stack-overflow post:

> <https://stackoverflow.com/questions/37661221/julia-select-all-but-one-element-in-array-matrix>

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [August 9, 2018, 12:20am UTC](https://discourse.julialang.org/t/extract-a-subarray-from-an-array-by-eliminating-certain-rows-and-columns/12974/5 "2018-08-09T00:20:55Z")

</div>

It’s nice syntax, but won’t that only work for eliminating a single row or column? Maybe I’m just not seeing what you’re implying, but the simplest generalization I can see off the top of my head is a loop over `in`.
