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[3]:=
mat = Array[Random[] & , {100, 100}];
I shall test separately two cases, when a<b and when a>b:
In[4]:=
a = 0.3; b = 0.5;
In[5]:=
Timing[p = funct1[a, b, mat]; ]
Out[5]=
{0.35 Second,Null}
In[6]:=
Timing[q = funct2[a, b, mat]; ]
Out[6]=
{0.02 Second,Null}
In[7]:=
p == q
Out[7]=
True
Now when a>b
In[8]:=
a = 0.5; b = 0.3;
In[9]:=
Timing[p = funct1[a, b, mat]; ]
Out[9]=
{0.3 Second,Null}
In[10]:=
Timing[q = funct2[a, b, mat]; ]
Out[10]=
{0.05 Second,Null}
In[11]:=
p == q
Out[11]=
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
>
>
>