"logo.gif"  
Main
Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node479_1.gif" "node479_2.gif" "node479_3.gif"

5.3.1.6    Example: unsorted union problem revisited  

One of the past examples for the <MapIndexed> function was to create an unsorted union of elements for some list. In particular, a set of rules for elements to their positions was constructed using MapIndexed. The code for the <unsortedUnion> function looked like:

"node479_4.gif"

The same rules can be created using Thread, which we will do now. This will be our test list:

"node479_5.gif"

"node479_6.gif"

This will create a set of rules similar to the one we have previously created by MapIndexed:

"node479_7.gif"

"node479_8.gif"

The only difference is that the positions are not wrapped in curly braces (lists), as they were for MapIndexed:

"node479_9.gif"

"node479_10.gif"

We could Map List on the result in our Thread-based realization, like this:

"node479_11.gif"

"node479_12.gif"

However, there is a more efficient realization - to keep the list as it is, but use Part instead of Extract (Part has a somewhat different syntax and in particular accepts a simple list of positions like the one generated by Thread). So, this is the final version:

"node479_13.gif"

Check:

"node479_14.gif"

"node479_15.gif"

We can now compare performance on some large list:    

"node479_16.gif"

"node479_17.gif"

"node479_18.gif"

"node479_19.gif"

"node479_20.gif"

The performance is roughly the same.

"node479_21.gif"

There are more capabilities of the Thread command. Some of them are discussed in Mathematica Help. We will eventually discuss them as we get to examples where they are useful.

"node479_22.gif" "node479_23.gif" "node479_24.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!