2
$\begingroup$

Say I want to run a function of the form

Table[i*j,{i,-5,5},{j,-5,5}]

but I want to omit all the values of $|i|\leq1$ and $|j|\leq1$. In other words, how do I create my 2D grid of values that doesn't contain the box of length 2 centered at the origin.

How would I write this in a general form rather than making the table by hand?

$\endgroup$
2
  • $\begingroup$ You mean you want to skip $i=-1,0,1$ and $j=-1,0,1$? $\endgroup$ – Nasser Apr 27 '20 at 16:37
  • $\begingroup$ Yes, exactly. I want to skip the 9 pairs ${i,j}$ where $i=-1,0,1$, $j=-1,0,1$ $\endgroup$ – flyingdutchman Apr 27 '20 at 16:40
3
$\begingroup$

And just for fun, here is another approach

Flatten[Table[If[Abs[i]<= 1 || Abs[j] <=1, Nothing[], i*j], {i, -5, 5}, {j, -5, 5}], {2}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ My question was probably not sufficiently clear. This is what I was looking for but exchanging the Or ( || ) for an And ( && ). Thanks! $\endgroup$ – flyingdutchman Apr 27 '20 at 17:20
2
$\begingroup$

One approach is to explicitly set the range for the variables:

ran = Select[Range[-5, 5], Abs[#] > 1 &];
Table[i*j, {i, ran}, {j, ran}]
$\endgroup$
2
$\begingroup$

Another one to try to get me to 10K.

With[{r = Join[#, -Reverse@#] &@Range[-5, -2]}, Table[i j, {i, r}, {j, r}]]
$\endgroup$
0

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.