Message Boards Message Boards

0
|
2368 Views
|
8 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Match data in two lists using DeleteCases[ ]?

I have two lists of numbers that are each 5,000 in length. Along with the numbers, I am also recording the original index numbers in a third column in both lists. Then I independently DeleteCases in each list based on certain criteria. Of course the original index numbers no longer match sequentially. What I want to do is further DeleteCases where the recorded index numbers no longer match in both lists so that eventually the original recorded index numbers match in both lists sequentially. Of course they are no longer sequential one by one but that doesn't matter. All that matters is that the events in both lists are matched up properly. Thanks for any help one can give. As I'm not sure if this can actually be done in Mathematica.

POSTED BY: Fred Diether
8 Replies

Sorry, I must have made a mistake when I first tried,

 list1third=list1[[All,3]];
 list2third=list2[[All,3]];

Because it does in fact work and is the same as,

list1third=Union[Map[#[[3]]&,list1]];
list2third=Union[Map[#[[3]]&,list2]];
POSTED BY: Fred Diether

Oops. What I got in the email works but what you posted here doesn't work.

list1third=Union[Map[#[[3]]&,list1]];
list2third=Union[Map[#[[3]]&,list2]];

I guess you did an edit after the email notification was sent. But those two lines work. Thanks again.

POSTED BY: Fred Diether

Many thanks, Bill. It works like a charm. Tried it with 10,000 rows for both lists. I knew that "Select" had to be involved but would have never guessed the rest of it. I will thoroughly study that so that I understand it.

Fred

POSTED BY: Fred Diether
Posted 3 years ago

Thank you for your patience. Now I think I understand. Try this

list1={
  {{0.955469,-0.295092,0},1,1},{{-0.999148,-0.0412787,0},-1,3},{{0.905129,0.425137,0},1,4},
  {{0.517574,0.855638,0},-1,6},{{-0.985269,-0.171011,0},1,8},{{-0.550035,0.835141,0},-1,9},
  {{-0.615241,-0.788339,0},-1,10},{{0.345384,0.938461,0},-1,11},{{0.973115,0.230318,0},1,12},
  {{-0.799272,0.600969,0},1,14},{{-0.690606,0.723231,0},-1,16},{{0.0841899,-0.99645,0},1,17},
  {{0.609325,0.792921,0},1,19},{{-0.274095,0.961703,0},1,20}};
list2={
  {{0.71463,-0.699502,0},-1,1},{{0.888074,-0.459701,0},-1,4},{{0.69891,-0.715209,0},-1,6},
  {{0.780179,0.625557,0},-1,7},{{0.715609,0.698501,0},-1,8},{{0.280162,0.959953,0},-1,10},
  {{0.176816,0.984244,0},-1,13},{{0.112615,0.993639,0},-1,15},{{0.922023,0.387135,0},-1,16},
  {{0.986248,-0.165273,0},-1,17},{{0.974008,-0.226515,0},-1,19}};
list1third=list1[[All,3]];
list2third=list2[[All,3]];
list1del=Select[list1,Intersection[{#[[3]]},list2third]=={#[[3]]}&]
list2del=Select[list2,Intersection[{#[[3]]},list1third]=={#[[3]]}&]

which should give you

{{{0.955469,-0.295092,0},1,1},{{0.905129,0.425137,0},1,4},{{0.517574,0.855638,0},-1,6},
 {{-0.985269,-0.171011,0},1,8},{{-0.615241,-0.788339,0},-1,10},{{-0.690606,0.723231,0},-1,16},
 {{0.0841899,-0.99645,0},1,17},{{0.609325,0.792921,0},1,19}}

and

{{{0.71463,-0.699502,0},-1,1},{{0.888074,-0.459701,0},-1,4},{{0.69891,-0.715209,0},-1,6},
 {{0.715609,0.698501,0},-1,8},{{0.280162,0.959953,0},-1,10},{{0.922023,0.387135,0},-1,16},
{{0.986248,-0.165273,0},-1,17},{{0.974008,-0.226515,0},-1,19}}

Does that do exactly what you want on this sample data? And does it do exactly what you want on your real data?

Please check this carefully Thanks

POSTED BY: Bill Nelson

Well, these two DeletedCases(DC) lists are easy because you can just do them manually. In the first DC list, you remove any rows that don't match the second DC list for the third column. Then in the second DC list remove any rows that don't match the first DC list for the third column. IOW, for example in the second DC list you would remove the rows that had for the third column 3,13,15 because they don't exist in the first DC list. Thanks.

POSTED BY: Fred Diether
Posted 3 years ago

Thank you.

Am I correct in thinking that the last two lists you just showed are the input to the final step that you don't know how to do and that you want to do another two DeleteCases, or perhaps something else, to get the final result?

If that is correct then can you show what the result should be after those two DeleteCases?

Perhaps from that someone can see how to reverse engineer the operation needed to automate that final step for you.

Thanks

POSTED BY: Bill Nelson

Sure. Below are the two lists with 20 rows each. Each list has a vector, scalar, recorded index number for each row. Then you can see the two lists after I have DeletedCases. Thanks.

POSTED BY: Fred Diether
Posted 3 years ago

Would you be able to make up some fake data, perhaps 10 rows for your first list and 12 rows for your second list, that have the same properties that your real data has after your first DeleteCases has finished? And then show and explain what the result of performing your last step on those two example tables would be? With that someone might be able to show a few lines of code that would do what you want.

POSTED BY: Bill Nelson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract