Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1879 > unrolled thread

Mathematica loop question

Started bySol Lederman <sol.lederman@gmail.com>
First post2011-04-26 08:43 +0000
Last post2011-04-27 09:36 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Mathematica loop question Sol Lederman <sol.lederman@gmail.com> - 2011-04-26 08:43 +0000
    Re: Mathematica loop question Stefan <wutchamacallit27@gmail.com> - 2011-04-26 10:50 +0000
    Re: Mathematica loop question Ray Koopman <koopman@sfu.ca> - 2011-04-26 10:50 +0000
      Re: Mathematica loop question Ray Koopman <koopman@sfu.ca> - 2011-04-27 09:36 +0000

#1879 — Mathematica loop question

FromSol Lederman <sol.lederman@gmail.com>
Date2011-04-26 08:43 +0000
SubjectMathematica loop question
Message-ID<ip60j0$aan$1@smc.vnet.net>
Hi,

I'm teaching myself Mathematica.

I've plotted this:

   Graphics[{
  Blue,
  Disk[{5, 10}, .25],
  Disk[{4, 9}, .25],
  Disk[{6, 9}, .25],
  Disk[{3, 8}, .25],
  Disk[{5, 8}, .25],
  Disk[{7, 8}, .25],
  Disk[{2, 7}, .25],
  Disk[{4, 7}, .25],
  Disk[{6, 7}, .25],
  Disk[{8, 7}, .25],
  Disk[{1, 6}, .25],
  Disk[{3, 6}, .25],
  Disk[{5, 6}, .25],
  Disk[{7, 6}, .25],
  Disk[{9, 6}, .25]
  }]


Is there an elegant way to write this without a bunch of Disk statements?
I'm imagining that this would be a double nested loop.


Thanks.


Sol

[toc] | [next] | [standalone]


#1883

FromStefan <wutchamacallit27@gmail.com>
Date2011-04-26 10:50 +0000
Message-ID<ip681p$blo$1@smc.vnet.net>
In reply to#1879
On Apr 26, 4:43 am, Sol Lederman <sol.leder...@gmail.com> wrote:
> Hi,
>
> I'm teaching myself Mathematica.
>
> I've plotted this:
>
>    Graphics[{
>   Blue,
>   Disk[{5, 10}, .25],
>   Disk[{4, 9}, .25],
>   Disk[{6, 9}, .25],
>   Disk[{3, 8}, .25],
>   Disk[{5, 8}, .25],
>   Disk[{7, 8}, .25],
>   Disk[{2, 7}, .25],
>   Disk[{4, 7}, .25],
>   Disk[{6, 7}, .25],
>   Disk[{8, 7}, .25],
>   Disk[{1, 6}, .25],
>   Disk[{3, 6}, .25],
>   Disk[{5, 6}, .25],
>   Disk[{7, 6}, .25],
>   Disk[{9, 6}, .25]
>   }]
>
> Is there an elegant way to write this without a bunch of Disk statements?
> I'm imagining that this would be a double nested loop.
>
> Thanks.
>
> Sol

Sol,

First of all, cheers to teaching yourself Mathematica, I'm also self
taught (and still learning). I'd recommend attending some of the
online seminars (free!) if you havent already.
Second, below is what I would do, though I have shifted your points
down by 5 (the top point is not at {5,10}, but {5,5} instead) because
this simplified things. A quick fix would just be to add this term
back in, but it makes no visual difference. The code is for a general
n, where n is the number of points in the bottom row, and consequently
the number of rows.

In[1]:= n = 5;
In[2]:= Graphics[Table[ Table[Disk[{x, y}, 0.25], {x, y, 2 n + 1 - y,
2}], {y, 1, n}]]

or just for fun (takes a few seconds)

In[6]:= n = 100;
In[7]:= Graphics[Table[Table[{Hue[(-2Mod[x,y])/n],Disk[{x,y},x*y/n^2]},
{x,y,2 n+1-y,2}],{y,1,n}],
              ImageSize->{621,325},Background->Black]


I did indeed use nested Table statements. Hope this helps!
-Stefan S

[toc] | [prev] | [next] | [standalone]


#1888

FromRay Koopman <koopman@sfu.ca>
Date2011-04-26 10:50 +0000
Message-ID<ip6814$bl6$1@smc.vnet.net>
In reply to#1879
On Apr 26, 1:43 am, Sol Lederman <sol.leder...@gmail.com> wrote:
> Hi,
>
> I'm teaching myself Mathematica.
>
> I've plotted this:
>
>    Graphics[{
>   Blue,
>   Disk[{5, 10}, .25],
>   Disk[{4, 9}, .25],
>   Disk[{6, 9}, .25],
>   Disk[{3, 8}, .25],
>   Disk[{5, 8}, .25],
>   Disk[{7, 8}, .25],
>   Disk[{2, 7}, .25],
>   Disk[{4, 7}, .25],
>   Disk[{6, 7}, .25],
>   Disk[{8, 7}, .25],
>   Disk[{1, 6}, .25],
>   Disk[{3, 6}, .25],
>   Disk[{5, 6}, .25],
>   Disk[{7, 6}, .25],
>   Disk[{9, 6}, .25]
>   }]
>
> Is there an elegant way to write this without a bunch of Disk statements?
> I'm imagining that this would be a double nested loop.
>
> Thanks.
>
> Sol

Flatten@Table[Disk[{n-7+2i,n},.25],{n,10,6,-1},{i,11-n}]

[toc] | [prev] | [next] | [standalone]


#1891

FromRay Koopman <koopman@sfu.ca>
Date2011-04-27 09:36 +0000
Message-ID<ip8o38$pdn$1@smc.vnet.net>
In reply to#1888
On Apr 26, 3:50 am, Ray Koopman <koop...@sfu.ca> wrote:
> On Apr 26, 1:43 am, Sol Lederman <sol.leder...@gmail.com> wrote:
>
>> Hi,
>>
>> I'm teaching myself Mathematica.
>>
>> I've plotted this:
>>
>>    Graphics[{
>>   Blue,
>>   Disk[{5, 10}, .25],
>>   Disk[{4, 9}, .25],
>>   Disk[{6, 9}, .25],
>>   Disk[{3, 8}, .25],
>>   Disk[{5, 8}, .25],
>>   Disk[{7, 8}, .25],
>>   Disk[{2, 7}, .25],
>>   Disk[{4, 7}, .25],
>>   Disk[{6, 7}, .25],
>>   Disk[{8, 7}, .25],
>>   Disk[{1, 6}, .25],
>>   Disk[{3, 6}, .25],
>>   Disk[{5, 6}, .25],
>>   Disk[{7, 6}, .25],
>>   Disk[{9, 6}, .25]
>>   }]
>>
>> Is there an elegant way to write this without a bunch of Disk statements?
>> I'm imagining that this would be a double nested loop.
>>
>> Thanks.
>>
>> Sol
>
> Flatten@Table[Disk[{n-7+2i,n},.25],{n,10,6,-1},{i,11-n}]

Flatten@Table[Disk[{i,n},.25],{n,10,6,-1},{i,n-5,15-n,2}]

is nicer.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web