0
$\begingroup$

I want to make a table with following data,

$a_{ij} = b_{ij}$ for $1\le i <j\le n-2$ and $|i-j| \ge 2$.

$\endgroup$

2 Answers 2

1
$\begingroup$

Try

n = 7;
Table[If[Abs[i - j] > 2, a[i, j] == b[i, j], Nothing], {i, 1,n - 2}, {j, i+1, n - 2}] // Flatten
(*{a[1, 4] == b[1, 4], a[1, 5] == b[1, 5], a[2, 5] == b[2, 5],a[4, 1] == b[4, 1], a[5, 1] == b[5, 1], a[5, 2] == b[5, 2]}*)    
$\endgroup$
7
  • $\begingroup$ Thanks! How to get only non-zero entries of the table? Otherwise it looks a bit clumsy. $\endgroup$ Jul 25, 2019 at 13:18
  • $\begingroup$ Without knowing a,b it's difficult to distinguish non-zero entries! $\endgroup$ Jul 25, 2019 at 13:22
  • $\begingroup$ I just meant the trivial 0 or Null entries that are appearing without referring to a or b.. $\endgroup$ Jul 25, 2019 at 13:33
  • 1
    $\begingroup$ Nothing inside If is what you're looking for. My answer is edited. $\endgroup$ Jul 25, 2019 at 13:39
  • $\begingroup$ Thanks! This is what I was looking for and I understand it too. $\endgroup$ Jul 25, 2019 at 13:42
1
$\begingroup$

Try

n = 7;
t=Table[a[i, j] == b[i, j], {j, 1,n - 2}, {i, 1,j - 2}]/.{}->Sequence[]

giving

{{a[1,3]==b[1,3]},
 {a[1,4]==b[1,4],a[2,4]==b[2,4]},
 {a[1,5]==b[1,5],a[2,5]==b[2,5],a[3,5]==b[3,5]}}

And you can use Flatten on that if you don't want the extra {}

$\endgroup$
3
  • $\begingroup$ This is great! But I have no clue how it's working! Can you explain that {}->Sequence[] part? Also can I generalize this to different conditions, say, |i-j| >= 5 or something.. $\endgroup$ Jul 25, 2019 at 13:31
  • 1
    $\begingroup$ If you try that without the /.{}->Sequence[] you will see that it leaves in "empty" rows where none of the items match both your conditions, so I used that "trick" to discard empty rows. For your generalized conditions try n = 12; t=Table[ a[i, j] == b[i, j], {j, 1,n - 2}, {i, 1,j - 5}]/.{}->Sequence[] $\endgroup$
    – Bill
    Jul 25, 2019 at 13:36
  • $\begingroup$ Yes, that's perfect. That should be imposed. $\endgroup$ Jul 25, 2019 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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