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


Groups > comp.soft-sys.math.mathematica > #16310

Re: Creating a list of 1x2 element arrays without procedural programming

From Murray Eisenberg <murray@math.umass.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Creating a list of 1x2 element arrays without procedural programming
Date 2013-12-02 06:52 +0000
Message-ID <l7hamg$pcs$1@smc.vnet.net> (permalink)
References <20131201132345.2493B6A0D@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


This is essentially the same question you asked, with a simpler example, 
in an earlier post. And non-procedural solutions would be similar. Of 
course you may use Table, again with Flatten at level 1 to remove one 
level of nesting:

    Flatten[Table[{5 i, -10 j}, {i, 6, 9}, {j, 27, 24, -1}], 1]

Alternatively, you may use Outer, again with Flatten:

    Flatten[Outer[List, 5 Range[6, 9], -10 Range[27, 24, -1]], 1]

There are various alternatives to the 2nd and 3rd arguments of Outer 
there. E.g., Range[30, 45, 5] and Range[-270, -240, 10].  In fact, for a 
larger-scale problem the latter are doubtless more efficient than what I 
showed, which involved multiplication.

Both solutions above give exact integers. You may then apply N to the 
entire result to get the approximate reals (with decimal points) that 
you requested.

     Flatten[Outer[List, 5 Range[6, 9], -10 Range[27, 24, -1]], 1] // N

which uses a postfix notation that abbreviates:

     N[Flatten[Outer[List, 5 Range[6, 9], -10 Range[27, 24, -1]], 1]]

Or you could use decimals in the ranges, e.g.:

     Flatten[Outer[List, Range[30., 45., 5], Range[-270., -240., 10]], 1]

I suspect (but did not try it) that at least for a larger-scale problem of this sort, it's more efficient just to apply N at the end.


On Dec 1, 2013, at 8:23 AM, amannucci <Anthony.J.Mannucci@jpl.nasa.gov> wrote:

> Is there a way in Mathematica using built-in functions to create what the following procedural program does?
>
> latlong = {}
> Do[AppendTo[latlong, {i, j}] , {i, 30.0, 45.0,
>   5.0}, {j, -270.0, -240.0, 10.0}];
> latlong
>
> Out:
> {{30., -270.}, {30., -260.}, {30., -250.}, {30., -240.}, {35., \
> -270.}, {35., -260.}, {35., -250.}, {35., -240.}, {40., -270.}, {40., \
> -260.}, {40., -250.}, {40., -240.}, {45., -270.}, {45., -260.}, {45., \
> -250.}, {45., -240.}}

---
Murray Eisenberg                                    
murray@math.umass.edu
Mathematics & Statistics Dept.      
Lederle Graduate Research Tower           
University of Massachusetts 
710 North Pleasant Street  
Amherst, MA 01003-9305





Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Creating a list of 1x2 element arrays without procedural programming Murray Eisenberg <murray@math.umass.edu> - 2013-12-02 06:52 +0000

csiph-web