1
$\begingroup$

I would like to find all row and column pairs that contain 1 in a table. For example, the row and column pairs that contain 1 in the table are (a,b),(b,a),(b,c),(c,b),(c,d),(d,c),(d,e),and (e,d).

TableForm[Table[Abs[i - j], {i, 5}, {j, 5}], TableHeadings -> {{a, b, c, d, e}, {a, b, c, d, e}}]
| improve this question | | | | |
$\endgroup$
2
$\begingroup$
table = Table[Abs[i - j], {i, 5}, {j, 5}];

rc = Position[table, 1] /. n_Integer :> {a, b, c, d, e}[[n]]

(* {{a, b}, {b, a}, {b, c}, {c, b}, {c, d}, {d, c}, {d, e}, {e, d}} *)
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Bob: Very efficient!! ... thank you! $\endgroup$ – PRG Apr 14 '19 at 1:55
3
$\begingroup$
A = Table[Abs[i - j], {i, 5}, {j, 5}];
{i, j} = Transpose[Position[A, 1]];
rowtitles = {a, b, c, d, e};
coltitles = {a, b, c, d, e};
Transpose[{rowtitles[[i]], coltitles[[j]]}]
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Henrik: Many thanks! $\endgroup$ – PRG Apr 14 '19 at 1:54
  • $\begingroup$ You're welcome! $\endgroup$ – Henrik Schumacher Apr 14 '19 at 7:21

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.