Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.2K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

populate a list based on output from multiple lists

Posted 10 years ago

Suppose you have the following lists

ans = {a1, a2, a3, a4, a5, a6};

bns = {b1, b2, b3, b4, b5, b6, b7, b8};

And the following output.

(* Out[69]= {26., {a1 -> 0., a2 -> 0., a3 -> 0., a4 -> 0., a5 -> 0., 
  a6 -> 1., b1 -> 0., b2 -> 0., b3 -> 0., b4 -> 0., b5 -> 1., 
  b6 -> 0., b7 -> 0., b8 -> 1.}} *)

How do I automatically create a list so that I have list of a and bs that were given the output of 1.

is this case,

{a6, b5, b8}

And if you have the following lists,

as = {3, 1, 4, 5, 5, 8};
an = {100, 50, 100, 150, 50, 100};
ans = {a1, a2, a3, a4, a5, a6};
bs = {3, 5, 1, 6, 8, 5, 5, 10};
bn = {100, 200, 150, 50, 150, 200, 50, 150};
bns = {b1, b2, b3, b4, b5, b6, b7, b8};

how will I populate a list to have the following structure based on that output from above?

{a6, 100, 8} (*a6 from "ans list", 100 from "an list" corresponding to a6, and 8 from "as list" also corresponding to a6 position.*) 

{b5, 150, 8,  b8, 150, 10} 

I need to learn to do it so that I can apply it to much longer list.

POSTED BY: bored dude
2 Replies
Posted 10 years ago

Thank you for the reply. And yes, the choice of entry in ans list with 1 will vary each time it's run. it might be 6 now in this example, but it might be a1 that's given 1. So I need a general way to choose the elements if they're given the value of 1 in the following pattern

ai-> 1 or bi-> 1...zi->1

Then Look up the corresponding values in the other lists given the chosen ai

As in the a6 is in the sixth position of the list and you want all corresponding numbers from the sixth position of the other lists? If yes, then

 pos = Position[ans, a6];
asVal = Extract[as, pos];
anVal = Extract[an, pos];
Flatten[{a6, anVal, asVal}]

This won't work since I have hundreds of list I need to make. This method means I have to type this out hundreds of times.

Keys@Select[Association @@ out[[2]], # == 1 &]

I don't have the newest mathematica so I don't have the functions you're using.

POSTED BY: bored dude

For some reason, your question is a bit hard for me to follow. For the second part of your question are you always going to have the list in the same order? As in the a6 is in the sixth position of the list and you want all corresponding numbers from the sixth position of the other lists? If yes, then

pos = Position[ans, a6];
asVal = Extract[as, pos];
anVal = Extract[an, pos];
Flatten[{a6, anVal, asVal}]

I think the first part is answered as follows

ans = {a1, a2, a3, a4, a5, a6}; 

bns = {b1, b2, b3, b4, b5, b6, b7, b8}; 

out = {26., {a1 -> 0., a2 -> 0., a3 -> 0., a4 -> 0., a5 -> 0., 
   a6 -> 1., b1 -> 0., b2 -> 0., b3 -> 0., b4 -> 0., b5 -> 1., 
   b6 -> 0., b7 -> 0., b8 -> 1.}}; 

Keys@Select[Association @@ out[[2]], # == 1 &]
POSTED BY: Kyle Keane
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard