Fast (compiled) routine for element testing and replacement in large matrices?
- To: mathgroup at smc.vnet.net
- Subject: [mg23929] Fast (compiled) routine for element testing and replacement in large matrices?
- From: "Gareth J. Russell" <russell at cerc.columbia.edu>
- Date: Fri, 16 Jun 2000 00:57:08 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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
==================================================