# remove certain columns from a matrix

I have a matrix A with n columns and matrix B with m < n columns. All columns of B are contained in the columns of A. I want to select those columns in A (the ones which are common in A and B) and remove them such that I am left with n-m columns in A. Is there a way to do this directly in Mathematica?

• Just a warning, you accepted an answer that produces an incorrect result (if your question is correctly phrased). Even after it was changed (to use transpose), it will reorder the columns. – Alan Apr 20 '18 at 18:58

If each element in B was contained in A, thenComplement[A,B] would do fine. For example, suppose A={{1, 2}, {3, 4}, {5, 6}} and B={{1, 2}, {3, 4}}, then it would be Complement[{{1, 2}, {3, 4}, {5, 6}}, {{1, 2}, {3, 4}}], which would give out {{5,6}} -----a correction--------- I mistaken the meaning of the word "column", the correct code should be Transpose@Complement[Transpose@A,Transpose@B]

• OP wanted columns, not rows. – Nasser Apr 20 '18 at 0:46
• Complement reorders ... – Alan Apr 20 '18 at 0:53

I'd prefer DeleteCases over replacement rules. Even faster is using Tally in conjunction with Pick.

n = 1000;
m = 100;
idx = RandomChoice[Range[n], m];
A = RandomInteger[{0, 1000}, {n, n}];
B = A[[All, idx]];

a = Transpose[ Transpose[A] /. {x_List /; MemberQ[Transpose[B], x] -> Nothing}]; //
RepeatedTiming // First
b = Transpose[DeleteCases[Transpose[A], Alternatives @@ Transpose[B]]]; //
RepeatedTiming // First
c = Transpose[
Pick[##, 1] & @@
Transpose[Tally[Join[Transpose[A], Transpose[B]]]]]; //
RepeatedTiming // First
a == b == c


0.364

0.018

0.010

True

You can use Fold to delete repeatedly from the transposed list, and transpose back to the original:

Transpose@Fold[DeleteCases, Transpose[A], Transpose[B]]

(* fake data *)
mA = RandomInteger[{0, 1000}, {10, 5}]
mB = mA[[All, {1, 3, 5}]]