Re: Using Extract where some indices are out of bounds (efficiently)
- To: mathgroup at smc.vnet.net
- Subject: [mg115453] Re: Using Extract where some indices are out of bounds (efficiently)
- From: Ray Koopman <koopman at sfu.ca>
- Date: Tue, 11 Jan 2011 06:59:22 -0500 (EST)
- References: <iev2kf$4mq$1@smc.vnet.net> <ifup7d$cog$1@smc.vnet.net> <ig97p9$1r6$1@smc.vnet.net>
On Jan 8, 12:38 am, Julian Francis <julian.w.fran... at gmail.com> wrote: > [...] > Thank you very much; that is excellent. My timings indicates your > version is approximately 40 times faster than my original extract > with arrayInBounds version. Here's an improved version of myExtract2 that seems a little faster, and a similar function, myExtract3, that may be faster still. myExtract2[array_,indices_] := Extract[array, Part[indices, SparseArray[Times@@UnitStep[(#-1)*(Dimensions@array-#)& @ Transpose@indices]] /. SparseArray[_,_,_,d_] :> Flatten@d[[2,2]] ]] myExtract3[array_,indices_] := Extract[array, Part[indices, SparseArray[Times@@MapThread[Clip[#1,{1,#2},{0,0}]&, {Transpose@indices, Dimensions@array}]] /. SparseArray[_,_,_,d_] :> Flatten@d[[2,2]] ]]