5
$\begingroup$

Consider the following rectangular matrix

M = {{1,1,1,1},{2,2,4,4},{3,3,5,5},{9,9,3,1}}

I would like to extract the position of equal columns. Is there a simple function or algorithm working with any matrix? By acting with a function (or using an algorithm) on M, I should get {1,2} as output.

More generally, there may be two sets of equal columns, as in the following case

 M = {{1,1,1,1},{2,2,4,4},{3,3,5,5},{9,9,3,3}}

in which case the output should be {{1,2},{3,4}}.

Is there something already implemented in Mathematica? I need a clever and fast algorithm which can be applied to big matrices of size 200x200.

$\endgroup$
7
$\begingroup$

You can use GatherBy and Select:

M = {{1, 1, 1, 1}, {2, 2, 4, 4}, {3, 3, 5, 5}, {9, 9, 3, 1}};

Select[GatherBy[Range[Length@M[[1]]], Transpose[M][[#]] &], Length[#] > 1 &]

{{1, 2}}

Also

 Select[Length @ # > 1&] @ Values @ PositionIndex[Transpose[M]]

{{1, 2}}

M2 = {{1, 1, 1, 1}, {2, 2, 4, 4}, {3, 3, 5, 5}, {9, 9, 3, 3}};

Select[GatherBy[Range[Length@M2[[1]]], Transpose[M2][[#]] &], Length[#] > 1 &]

{{1, 2}, {3, 4}}

 Select[Length @ # > 1&] @ Values @ PositionIndex[Transpose[M2]]

{{1, 2}, {3, 4}}

$\endgroup$
  • $\begingroup$ Umh.. this does not work well. For example, the matrix posted here pastebin.com/BJZ6VmKe has the columns 14,17,24 equal.I think you should modify the command as Select[GatherBy[Range[Length@Transpose@M], Transpose[M][[#]] &], Length[#] > 1 &], right? $\endgroup$ – apt45 Jul 21 at 18:08
  • 1
    $\begingroup$ @apt45, thank you. Fixed it. $\endgroup$ – kglr Jul 21 at 18:10
  • $\begingroup$ Thank to you ;) ! $\endgroup$ – apt45 Jul 21 at 18:11
  • $\begingroup$ Hi again! Is there any way to speed up the algorithm when dealing with ~60000 columns and ~60 rows? Thanks! $\endgroup$ – apt45 Aug 21 at 23:13
  • $\begingroup$ Values@PositionIndex[Transpose[M]] is much more performant! I would suggest to add this to the answer. Cheers. $\endgroup$ – apt45 Aug 25 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.