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

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

```Here is an improved version of funct2 which considerably speeds up the
case b<=a:

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

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

On Sunday, July 28, 2002, at 06:32  PM, Andrzej Kozlowski wrote:

> 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
>
>
>
>
> 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: Followup on Graphics3D rendering problem
• Next by Date: Re: Work with Contexts
• Previous by thread: Re: Max[Min[#,b],a]&
• Next by thread: Best way to convert graphics to slide show? (Mac OS 9)