       Re: more looping trouble

• To: mathgroup at smc.vnet.net
• Subject: [mg85479] Re: more looping trouble
• From: yatesd at mac.com
• Date: Mon, 11 Feb 2008 06:21:15 -0500 (EST)
• References: <fomjsd\$hsj\$1@smc.vnet.net>

```Not sure I fully understand what you are trying to do, but here are a
few suggestions:

(1) Loops are rarely used in Mathematica. It takes awhile to learn how
to do things you are used to doing with loops, but once you know how,
the code is usually compacter, easier to read, and usually much much
faster

(2) Append and Prepend are quite slow operations (not for a data set
that is your size, but very noticeable on big data sets). There is
often a better way to do it.

Here are some suggestions for what you are doing:

data2D = {{4.4, 14.}, {6.7, 15.25}, {6.9, 12.8}, {2.1, 11.1}, {9.5,
14.9}, {13.2, 11.9}, {10.3, 12.3}, {6.8, 9.5}, {3.3, 7.7}, {0.6,
5.1}, {5.3, 2.4}, {8.45, 4.7}, {11.5, 9.6}, {13.8, 7.3}, {12.9,
3.1}, {11., 1.1}}

(Note I have replace 14 with 14. in position {1,2} and similarly in 11-
>11. in position {16,1}. This is not necessary, but now all your
numbers are Reals, rather than a mix of Reals and Integers.)

Here is an alternative way to create data3D

data3D = Replace[data2D, {x_, y_} :> {x, y, Sqrt[64 - (x - 8)^2 - (y -
8)^2]}, {1}]

This uses a replacement rule which I find easier to understand,
although there is nothing wrong with your way either. Another
alternative would be

data3D = {#[],#[],Sqrt[64 - (#[] - 8)^2 - (#[] - 8)^2]}&/
@data2D

dat = Sin data3D
(Note that in your output, the 2 Integers in data2D become 14 Sin
and 11 Sin in dat, which you might have wondered about. Mathematica
leaves them in this form since they are exact quantities. Reals are
approximate values and so the Sin is converted to a Real and the
multiplication is then done)

Next you say you want to delete the 3rd number. Here are some
alternatives:

(1) Take just the first 2 numbers: k = dat[[All,1;;2]] (Mathematica 6
only, for older versions dat[[All,{1,2}]]
(2) k = Sin * data2D seems to be what you are after, so perhaps you
could do that directly

The reason your loop failed was that k is of length 3, but you are
trying to assign to parts 4,5,...,16 of k

Here are some other ways closer to the spirit of your loop, although

(3) k = Delete[#,3]&/@data3D
(4) k = Take[#,2]&/@data3D
(5) k = {#[],#[]}&/@data3D

Hope that helps,

Derek

```

• Prev by Date: Re: Creating notebook templates
• Next by Date: Re: Integrating x^b*Log[x]^m gives wrong result?
• Previous by thread: Re: more looping trouble
• Next by thread: Re: more looping trouble