Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Creating a list of 1x2 element arrays without
Replies: 4   Last Post: Dec 2, 2013 1:52 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Murray Eisenberg

Posts: 2,099
Registered: 12/6/04
Re: Creating a list of 1x2 element arrays without procedural programming
Posted: Dec 2, 2013 1:52 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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









Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.