# Produce a 1-dimensional list of all unique matrix elements

We have some symbolic matrix m, e.g.

m={{1, x, 4 x + y},{0, x y + 4x, 4x},{7 x, x, 4x + x y}}


and we want to produce as output a list of all the unique elements present in m:
desired output:

{1, x, 4 x + y, 0, x y + 4x, 4x, 7 x}


I've tried to use ArrayReshape and ArrayFlatten to convert the matrix into a 1-dimensional list, and then I would hopefully be able to conclude with Union, but so far it isn't working properly.

This should work:

DeleteDuplicates@Flatten@m


{1, x, 4 x + y, 0, 4 x + x y, 4 x, 7 x}

If you want the result sorted, then you can replace DeleteDuplicates with Union:

Union@Flatten@m


{0, 1, x, 4 x, 7 x, 4 x + y, 4 x + x y}

• Wonderful, works as expected. Thanks. Feb 4, 2014 at 19:59
• It's curious, with mathematica having such a large number of functions; I've known of the existence of ArrayFlatten for months but I didn't know about Flatten which turned out to be the trick. Other than just experience fighting with this stuff, is there a good way one comes to learn what mathematica functions are out there? Feb 4, 2014 at 20:00
• @Steve, one way is to devote some time learning how to use Mathematica. This should get you started: mathematica.stackexchange.com/questions/18/… Feb 4, 2014 at 20:12
• @Steve I agree that Mathematica has a bewildering number of functions and it's a little difficult to get to know that "essential set" that can be a good basis for solving any problem ... if such a thing exists at all. It's a good idea to sometimes browse the documentation pages and just look at what's available. I.e. not look for a function when you need it, but casually look at what's available and what each function does. The first four sections here are worth skimming through, most of it is rather "low level". Feb 5, 2014 at 0:43
• Low level in the sense that most of those functions are quite fundamental. I like to recommend this course. It has exercises which is good. It's very old, but that can be a good thing: the functions you'll learn from it are the most basic ones, and they still work. There's a large number of later additions, most of them higher level. Feb 5, 2014 at 0:45