Message Boards Message Boards

0
|
3814 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Create a thread from two random arrays of elements?

Posted 4 years ago

Hi,

I have two arrays of elements say, A = {1, 2} and B = {3, 4} for a working example. The way they are generated in a multi-step process, there is no control over the order of elements. So A may also be {2, 1} and so is true for B.

Now I want to create one-to-one thread from A -> B, which would give o/p either {1-> 3, 2-> 4} or {1-> 4, 2-> 3}. By using Permutations[B] in stead of B, one can generate all possible threads.

Now I want to pick one of those, which is more likely. For above example, I know that 1-> 4 is more likely than 1-> 3 so the correct thread would be the one with it so {1-> 4, 2-> 3} should be preferred over {1-> 3, 2-> 4} as o/p. Now my question is how to do this mathematically. How to tell mathematical that out of n possible elements in an table, pick one over all others (n-1) depending upon the preference of i->j present in the elements. Following is the code for working example. SelectFirst seems to be a good choice for doing it, but I could not code the condition part properly.

Will appreciate any help on this, or any other alternative function to get this done. Thanks

A = {1, 2};
B = {3, 4};
BB = Permutations[B];
CC = Table[Thread[A -> BB[[i]]], {i, 1, Length[B]}]
POSTED BY: S G
3 Replies

I am afraid I don't quite understand what you want. Perhaps this?

A = {1, 2, 3};
B = {4, 5, 6};
BB = Permutations[B];
CC = Table[Thread[A -> BB[[i]]], {i, 1, Length[B]}]
pref = Rule[1, 4];

with = Select[CC, MemberQ[#, pref] &]
without = Select[CC, Not[MemberQ[#, pref]] &]
POSTED BY: Hans Dolhaine
Posted 4 years ago

Thanks, pretty much yes. But two small points -

(1) Is there an option opposite to "Select", which can leave out the options according to the Rule, rather than selecting. (Otherwise one can always subtract the o/p of "Select" from original string to get this effect)

(2) Also, I want to set preferences. So that if I use Rule[3, 1], I get o/p without it if possible, if not, I get one with it. On the contrary "Select" completely bars the Rule and if there is no possible o/p without 3->1, I get null o/p. I don't want this.

Hope I am able to explain both my points. Will appreciate you suggestion on them.

POSTED BY: S G

Do you perhaps mean this?

A = {1, 2, 3};
B = {4, 5, 6};
BB = Permutations[B];
CC = Table[Thread[A -> BB[[i]]], {i, 1, Length[B]}]
pref = Rule[1, 4];
Select[CC, MemberQ[#, pref] &]
POSTED BY: Hans Dolhaine
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