Re: choose elements from list based on elements in different list
- To: mathgroup at smc.vnet.net
- Subject: [mg86734] Re: choose elements from list based on elements in different list
- From: "David Park" <djmpark at comcast.net>
- Date: Thu, 20 Mar 2008 02:50:34 -0500 (EST)
- References: <frqpnb$4vl$1@smc.vnet.net>
Whenever one wants to do operations on two equal length structures the Inner statement is a good choice. Reihe1 = {22, 33, 44, 0, 0, 16, 5, 0, 0, 0}; Reihe2 = {16, 27, 0, 0, 5, 11, 5, 0, 0, 12}; Here we do the two tests and either insert the nonzero number in a new list, or insert an empty Sequence. The empty Sequence must be Unevaluated in the If statement or it would simply disappear for there. Inner[If[#1 == 0 \[And] #2 != 0, #2, Unevaluated@Sequence[]] &, Reihe1, Reihe2, List] {5, 12} Inner[If[#2 == 0 \[And] #1 != 0, #1, Unevaluated@Sequence[]] &, Reihe1, Reihe2, List] {44} This construction is also useful for the combination of equations. eqn1 = x == a + b; eqn2 = y == a - b; Inner[#1 + #2 &, eqn1, eqn2, Equal] x + y == 2 a Inner[Expand[2 #1 - #2] &, eqn1, eqn2, Equal] 2 x - y == a + 3 b -- David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ "Claus" <clausenator at gmail.com> wrote in message news:frqpnb$4vl$1 at smc.vnet.net... > Hi, > I'm trying to choose elements from list based on elements in different > list and I came up with the ideas listed below. It seems to me that > there are a few too many steps involved here, and I need to run this on > larger lists (~20000 entries). > Are there ways to improve this? > Thanks, > Claus > > Here is the mathematica code: > > Reihe1={22,33,44,0,0,16,5,0,0,0} > Reihe2={16,27,0,0,5,11,5,0,0,12} > > {22, 33, 44, 0, 0, 16, 5, 0, 0, 0} > > {16, 27, 0, 0, 5, 11, 5, 0, 0, 12} > > (*Find out where in Reihe1 and in Reihe2 are Zeros and notZeros*) > > Reihe1Is0 = Thread[Reihe1 == 0] > Reihe1Not0 = Thread[Reihe1 != 0] > Reihe2Is0 = Thread[Reihe2 == 0] > Reihe2Not0 = Thread[Reihe2 != 0] > > {False, False, False, True, True, False, False, True, True, True} > > {True, True, True, False, False, True, True, False, False, False} > > {False, False, True, True, False, False, False, True, True, False} > > {True, True, False, False, True, True, True, False, False, True} > > (*Select the values of Reihe1 and Reihe2 which are of interest > > There are 4 Cases (not all of them calculated later) : > Case1 : Reihe1 and Reihe2 are 0 > Case2 : Reihe1 and Reihe2 are NOT 0 > Case3 : (Reihe2 is NOT 0) AND (Reihe1 is 0) > Case4 : (Reihe1 is NOT 0) AND (Reihe2 is 0)*) > > PreCase3=Pick[Reihe2,Reihe1Is0] > PreCase4=Pick[Reihe1,Reihe2Is0] > > {0, 5, 0, 0, 12} > > {44, 0, 0, 0} > > Case3=Select[PreCase3,#>0&] > Case4=Select[PreCase4,#>0&] > > {5, 12} > > {44} >