Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

This should work:


{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:


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

share|improve this answer
Wonderful, works as expected. Thanks. – Steve Feb 4 '14 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? – Steve Feb 4 '14 at 20:00
@Steve, one way is to devote some time learning how to use Mathematica. This should get you started:… – RunnyKine Feb 4 '14 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". – Szabolcs Feb 5 '14 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. – Szabolcs Feb 5 '14 at 0:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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