Re: A fast way to compare two vectors
- To: mathgroup at smc.vnet.net
- Subject: [mg121688] Re: A fast way to compare two vectors
- From: Ray Koopman <koopman at sfu.ca>
- Date: Mon, 26 Sep 2011 04:13:43 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j5m44t$ple$1@smc.vnet.net> <j5mt8t$sn8$1@smc.vnet.net>
On Sep 25, 2:46 am, Ray Koopman <koop... at sfu.ca> wrote: > [...] > poscom[a,b] assumes that a and b are sorted lists of positive > integers, with no within-list duplicates. It returns the positions > in a of those values that are not also in b. It is equivalent to > Flatten[Position[a,#]&/@Complement[a,b]]. > > poscom[a_,b_] := Block[{r = ConstantArray[0,Max[a[[-1]],b[[-1]]]]}, > r[[a]] = Range@Length@a; r[[b]] = ConstantArray[0,Length@b]; > SparseArray[r] /. SparseArray[_,_,_,d_] :> d[[3]] ] This may be faster than poscom for some data: poskom[a_,b_] := Block[{r = Normal@SparseArray[ Automatic, {Max[a[[-1]],b[[-1]]]}, 0, {1, {{0, Length@a}, Transpose@{a}}, Range@Length@a} ]}, r[[b]] = ConstantArray[0,Length@b]; SparseArray[r] /. SparseArray[_,_,_,d_] :> d[[3]] ]