Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

1 Answer 1

up vote 1 down vote accepted

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}

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

Your Answer

 
discard

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.