4
$\begingroup$

I want to learn some tips to deal data.
If i want connect two data with Rule.

a = Array[Sqrt, 10]; b = Array[Sin, 10];
Thread[a -> b]

Then i gets.

{1 -> Sin[1], Sqrt[2] -> Sin[2], Sqrt[3] -> Sin[3], 2 -> Sin[4], 
 Sqrt[5] -> Sin[5], Sqrt[6] -> Sin[6], Sqrt[7] -> Sin[7], 
 2 Sqrt[2] -> Sin[8], 3 -> Sin[9], Sqrt[10] -> Sin[10]}

I want to extract the result.That means an inverse function of Thread.

That isFun[Thread[a -> b]]->{a,b}.
I know a solution is a=d[[;;,1]];b=d[[;;,2]].
Is there a better?

$\endgroup$
1
  • $\begingroup$ Proposed duplicate: (130093) $\endgroup$
    – Mr.Wizard
    Apr 11, 2017 at 12:24

2 Answers 2

6
$\begingroup$

You can use Thread a second time:

a = Array[Sqrt, 10]; b = Array[Sin, 10];
x = Thread[a -> b];

{aa, bb} = List @@ Thread[x, Rule];

aa === a
bb === b
True

True

Or a bit more concisely, but perhaps not in the spirit you seek:

{aa, bb} = Thread[List @@@ x];
$\endgroup$
1
  • $\begingroup$ I find a strange thing. ``` resource = ResourceObject["MNIST"]; trainingData = ResourceData[resource, "TrainingData"]; fe[image_] := ImageData[image] // Flatten; {a, b} = Thread[List @@@ trainingData ]; trainingData1 = Thread[(fe/@a) -> b] ``` It needs lots of memories and will not stop. $\endgroup$
    – erow
    Apr 2, 2017 at 4:53
6
$\begingroup$

You'll get lots of variants. I always use Part like in your example. However one way, different to above, admittedly not an "UnThread" is:

{a, b} = Transpose[x /. Rule -> List]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.