       Re: Max[Min[#,b],a]&

• To: mathgroup at smc.vnet.net
• Subject: [mg35749] Re: [mg35746] Max[Min[#,b],a]&
• From: Andrzej Kozlowski <andrzej at bekkoame.ne.jp>
• Date: Mon, 29 Jul 2002 03:13:22 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Okay, here is my first attempt.

Here is what I take to be the "test function":

funct1[a_, b_, mat_] := Map[Max[Min[#, b], a] &, mat, {2}];

here is my challenger:

funct2[a_, b_, mat_] := If[b <= a,
Array[a & , Dimensions[mat]],
Map[Which[#1 <= a, a, #1 <= b, #1, True, b] & , mat, {2}]]

We create a random matrix of entries:

In:=
mat = Array[Random[] & , {100, 100}];

I shall test separately two cases, when a<b and when a>b:

In:=
a = 0.3; b = 0.5;

In:=
Timing[p = funct1[a, b, mat]; ]

Out=
{0.35 Second,Null}

In:=
Timing[q = funct2[a, b, mat]; ]

Out=
{0.02 Second,Null}

In:=
p == q

Out=
True

Now when a>b

In:=
a = 0.5; b = 0.3;

In:=
Timing[p = funct1[a, b, mat]; ]

Out=
{0.3 Second,Null}

In:=
Timing[q = funct2[a, b, mat]; ]

Out=
{0.05 Second,Null}

In:=
p == q

Out=
True

Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/

On Sunday, July 28, 2002, at 04:32  PM, Selwyn Hollis wrote:

> Since Mathgroup had so much fun with the recent problem of counting
> occurences of ...,1,0,... in a list of zeros and ones, I thought I'd try
> to bring the collective intelligence to bear on the following:
>
> Given a matrix M of real numbers and a pair of real numbers a and b,
> what is the most efficient way to achieve the effect of applying the
> function Max[Min[#,b],a]& to each number in M?
>
> ----
> Selwyn Hollis
> slhollis at mac.com
>
>
>

```

• Prev by Date: Re: A faster alternative to ListIntegrate? 
• Next by Date: Followup on Graphics3D rendering problem