Re: Fast (compiled) routine for element testing and replacement in large matrices?
- To: mathgroup at smc.vnet.net
- Subject: [mg23978] Re: Fast (compiled) routine for element testing and replacement in large matrices?
- From: "Bob Abraham" <abraham at ast.cam.ac.uk>
- Date: Sun, 18 Jun 2000 03:01:01 -0400 (EDT)
- References: <8iccl1$9oj@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Gareth ---
This way should be much faster:
d2wreal[w_, l_, u_] := Map[If[(# > l) && (# <= u), 1, 0] &, w, {2}]
With Mathematica 4 on my 450Mhz PIII your example with a matrix size of
1000x1000 takes 1.9s.
Regards,
Bob Abraham
"Gareth J. Russell" <russell at cerc.columbia.edu> wrote in message
news:8iccl1$9oj at smc.vnet.net...
> Hello,
>
> My first post. I am moving from another software package to Mathematica,
and
> I often work with very large matrices in the context of simulations, so
> speed is important. Here is a problem for which I would like a serious
speed
> increase.
>
> Given a matrix of real numbers, I want to generate a new matrix where
> values from a certain range are replaced with 1, and the remainder with
> zero. Matrix size is often on the order of 1000 by 1000 (they are always
> square).
>
> I defined a function that does the comparison using If[],
> and made it listable:
>
> d2wreal[w_,l_,u_] := If[(w>l)&&(D<=u),1,0]
> SetAttributes[d2wreal,Listable]
>
> These are typical times:
>
> a = Table[Random[], {i, 1, 100}, {j, 1, 100}];
> t = Timing[b = d2wreal[a, 0.2, 0.4]][[1]]
> 0.46666666666666856 Second
>
> a = Table[Random[], {i, 1, 1000}, {j, 1, 1000}];
> t = Timing[b = d2wreal[a, 0.2, 0.4]][[1]]
> 65.55 Second
>
> I tried to generate some compiled alternatives myself, but I couldn't find
> one that was faster. I reckon a 10x speed-up should be possible, possibly
> more. My other software takes only 0.75 seconds for the 1000 by 1000
matrix
> function.
>
> Can anyone suggest a way to speed this up?
>
> Thanks,
>
> Gareth
>
>
> ==================================================
> Dr. Gareth J. Russell
>
> NEW ADDRESS and E-MAIL FROM 1ST SEPTEMBER, 1999
>
> Center for Environmental Research and Conservation
> MC 5556
> Columbia University
> 1200 Amsterdam Avenue
> New York, NY 10027, U.S.A.
>
> Phone: ++1 212 854 5094
> Fax: ++1 212 854 8188
> E-mail: russell at cerc.columbia.edu
> WWW: http://web.utk.edu/~grussell (NO CHANGE)
>
> OLD ADDRESS (AND STILL MY EMPLOYERS)
>
> Department of Ecology and Evolutionary Biology
> University of Tennessee
> 569 Dabney Hall
> Knoxville, TN 37996-1610, USA
> ==================================================
>
>
>
- Follow-Ups:
- Re : Fast (compiled) routine for element testing and replacement in large matrices? A more general question about Listable
- From: Jacqueline Zizi <jazi@club-internet.fr>
- Re : Fast (compiled) routine for element testing and replacement in large matrices? A more general question about Listable