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


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?



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.


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))]


Check out this stack-overflow post:


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.