Matrix indexing

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.

A[:,4:end]

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

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.

5 Likes

alternatively something like this

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

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

1 Like

Suggestion:

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
3 Likes