Re: A fast way to compare two vectors
- To: mathgroup at smc.vnet.net
- Subject: [mg121694] Re: A fast way to compare two vectors
- From: Yasha Gindikin <gindikin at gmail.com>
- Date: Mon, 26 Sep 2011 04:14:49 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j5m44t$ple$1@smc.vnet.net> <j5mt5u$smm$1@smc.vnet.net>
- Reply-to: comp.soft-sys.math.mathematica at googlegroups.com
Dear Oleksandr, thank you very much for your code. Actually, I was trying to implement the same algorithm. My code was the following: Compare := Compile[{{a, _Integer, 1}, {b, _Integer, 1}}, {s1 = s2 = 0; k = n; d1 = d2 = Table[0, {k, 3}]; While[s1 < 3 && s2 < 3 && k - s1 > 0 && k - s2 > 0, Which[a[[k - s1]] > b[[k - s2]], d1[[s1 + 1]] = a[[k - s1]]; s1++, a[[k - s1]] < b[[k - s2]], d2[[s2 + 1]] = b[[k - s2]]; s2++, a[[k - s1]] == b[[k - s2]], k--]]; Cm = Max[{s1, s2}]; Which[Cm > 2, False, s1 - s2 == 1, d2[[s1]] = b[[1]], s2 - s1 == 1, d1[[s2]] = a[[1]], s1 - s2 == 2, d2[[1]] = b[[2]]; d2[[2]] = b[[1]], s2 - s1 == 2, d1[[1]] = a[[2]]; d1[[2]] = a[[1]]]; Cm}]; But due to my very limited programming experience, the code turned out to be not very efficient. It even works faster without the Compile option, which puzzles me a lot. I will try to use your realization instead. Thank you once again! Regards, Yasha.