       Re: an even faster way to normalize a

• To: mathgroup at smc.vnet.net
• Subject: [mg85669] Re: an even faster way to normalize a
• From: m.r at inbox.ru
• Date: Tue, 19 Feb 2008 01:50:20 -0500 (EST)
• References: <fp9945\$199\$1@smc.vnet.net>

```On Feb 17, 6:25=A0am, congruentialumina... at yahoo.com wrote:
> Hello UG:
>
> I have a 512x512 array of 3-tuples. I want to make any tuple with a
> value outside of 0 <--> 1, become {0.,0.,0.}.
>
> The first version has this loop:
>
> For[i = 1, i <= graphSize, i++,
> =A0 For[j = 1, j <= graphSize, j++,
> =A0 =A0If[((sum[[i, j, 1]] < 0) || (sum[[i, j, 1]] > 1) ||
> =A0 =A0 =A0 (sum[[i, j, 2]] < 0) || (sum[[i, j, 2]] > 1) ||
> =A0 =A0 =A0 (sum[[i, j, 3]] < 0) || (sum[[i, j, 3]] > 1)),
> =A0 =A0 sum[[i, j]] = {0., 0., 0.}
> =A0 =A0 ]
> =A0 =A0]
> =A0 ];
>
> After scratching my head for a while I came up with this (equivalent)
> Map statement.
>
> sum = Map[
> =A0 =A0If[#[] < 0 || #[] > 1 || #[] < 0 || #[] > 1 || #[] <=

> =A0 =A0 =A0 =A0 0 || #[] > 1, {0., 0., 0.}, #] &, sum, {2}];
>
> It is faster but only by about 15%.
>
> It is unreasonable to believe some other construction can accomplish
> this with a bigger payoff?
>
>
> Regards..Roger W.

In version 6:

sum = RandomReal[{-1, 2}, {512, 512, 3}];

ans = sum N[Times @@ Unitize@ Clip[
Transpose[sum, {2, 3, 1}], {0., 1.}, {0., 0.}]];

N is needed to make sure that the result of the multiplication is a
(packed) array of reals.

Maxim Rytin
m.r at inbox.ru

```

• Prev by Date: .NET/Link: Explicitly define data type to be sent to .NET object?
• Next by Date: C++ const and mathlink
• Previous by thread: Re: .NET/Link: Explicitly define data type to be sent to .NET object?
• Next by thread: Re: an even faster way to normalize a