0
$\begingroup$

Assume we have the following matrix/tensor. How can I test if the column has the same element? 4-6 and 8-9 columns has the same element.

$ \left( \begin{array}{cccccccccc} \{2,3\} & \{2,3\} & \{2,4\} & \{2,4\} & \{2,4\} & \{2,4\} & \{2,3\} & \{2,3\} & \{2,3\} & \{2,3\} \\ \{2,3\} & \{2,4\} & \{2,4\} & \{2,4\} & \{2,4\} & \{2,4\} & \{2,3\} & \{2,3\} & \{2,3\} & \{1,3\} \\ \{1,3\} & \{2,4\} & \{1,3\} & \{2,4\} & \{2,4\} & \{2,4\} & \{2,3\} & \{2,3\} & \{2,3\} & \{2,3\} \\ \{2,3\} & \{2,4\} & \{1,3\} & \{2,4\} & \{2,4\} & \{2,4\} & \{1,3\} & \{2,3\} & \{2,3\} & \{2,3\} \\ \end{array}\right)$

mat={{{2, 3}, {2, 3}, {2, 4}, {2, 4}, {2, 4}, {2, 4}, {2, 3}, {2, 3}, {2, 
   3}, {2, 3}}, {{2, 3}, {2, 4}, {2, 4}, {2, 4}, {2, 4}, {2, 4}, {2, 
   3}, {2, 3}, {2, 3}, {1, 3}}, {{1, 3}, {2, 4}, {1, 3}, {2, 4}, {2, 
   4}, {2, 4}, {2, 3}, {2, 3}, {2, 3}, {2, 3}}, {{2, 3}, {2, 4}, {1, 
   3}, {2, 4}, {2, 4}, {2, 4}, {1, 3}, {2, 3}, {2, 3}, {2, 3}}};

I have tried this but it didn't work.

AllTrue[mat[[All, #]], SameQ] & /@ Range@10
$\endgroup$
  • 1
    $\begingroup$ SameQ @@@ Transpose[mat]? $\endgroup$ – J. M. is away Mar 31 '18 at 5:53
  • $\begingroup$ Yes, that works perfectly. Thanks. $\endgroup$ – Okkes Dulgerci Mar 31 '18 at 6:19
  • $\begingroup$ For larger matrices, integer arithmetic (performed in the reals) might be more performant: {True, False}[[ Unitize[ (ConstantArray[1., Dimensions[A][[1]] - 1].Abs[ Differences[A]]).ConstantArray[1., Dimensions[A][[3]]] ] + 1]]. $\endgroup$ – Henrik Schumacher Mar 31 '18 at 8:55
  • $\begingroup$ My matrix is at most 10x90 $\endgroup$ – Okkes Dulgerci Mar 31 '18 at 15:02
  • $\begingroup$ Then it's probably not worth the effort. $\endgroup$ – Henrik Schumacher Mar 31 '18 at 20:20

Your Answer

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

Browse other questions tagged or ask your own question.