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$
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]}*)    
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks! How to get only non-zero entries of the table? Otherwise it looks a bit clumsy. $\endgroup$ – Physics Moron Jul 25 '19 at 13:18
  • $\begingroup$ Without knowing a,b it's difficult to distinguish non-zero entries! $\endgroup$ – Ulrich Neumann Jul 25 '19 at 13:22
  • $\begingroup$ I just meant the trivial 0 or Null entries that are appearing without referring to a or b.. $\endgroup$ – Physics Moron Jul 25 '19 at 13:33
  • 1
    $\begingroup$ Nothing inside If is what you're looking for. My answer is edited. $\endgroup$ – Ulrich Neumann Jul 25 '19 at 13:39
  • $\begingroup$ Thanks! This is what I was looking for and I understand it too. $\endgroup$ – Physics Moron Jul 25 '19 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 {}

| improve this answer | |
$\endgroup$
  • $\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$ – Physics Moron Jul 25 '19 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 '19 at 13:36
  • $\begingroup$ Yes, that's perfect. That should be imposed. $\endgroup$ – Physics Moron Jul 25 '19 at 13:40

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.