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:


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

| improve this answer | |
  • $\begingroup$ Wonderful, works as expected. Thanks. $\endgroup$ – Steve Feb 4 '14 at 19:59
  • $\begingroup$ 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? $\endgroup$ – Steve Feb 4 '14 at 20:00
  • 1
    $\begingroup$ @Steve, one way is to devote some time learning how to use Mathematica. This should get you started: mathematica.stackexchange.com/questions/18/… $\endgroup$ – RunnyKine Feb 4 '14 at 20:12
  • 1
    $\begingroup$ @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". $\endgroup$ – Szabolcs Feb 5 '14 at 0:43
  • 1
    $\begingroup$ 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. $\endgroup$ – Szabolcs Feb 5 '14 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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