1

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.

Steve
  • 1,153
  • 7
  • 17

1 Answers1

1

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}

rm -rf
  • 88,781
  • 21
  • 293
  • 472
s0rce
  • 9,632
  • 4
  • 45
  • 78
  • Wonderful, works as expected. Thanks. – Steve Feb 04 '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 04 '14 at 20:00
  • 1
    @Steve, one way is to devote some time learning how to use Mathematica. This should get you started: http://mathematica.stackexchange.com/questions/18/where-can-i-find-examples-of-good-mathematica-programming-practice – RunnyKine Feb 04 '14 at 20:12
  • @Steve: If you're looking for a way to expand your "vocabulary" of built-in functions, the best way is to use the documentation in Help>Documentation Center. Each entry is an actual editable Mathematica notebook, and at the bottom of each article are hyperlinks to other related functions. For example, you say you knew about ArrayFlatten but not Flatten; Flatten is actually the very first hyperlink at the bottom of the instruction page for ArrayFlatten. You can surf the documentation like you would the internet, going from function to function. – DumpsterDoofus Feb 04 '14 at 21:05
  • 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 05 '14 at 00: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 05 '14 at 00:45
  • I appreciate the resources, I plan to delegate a bit of research time to these sorts of tasks daily. – Steve Feb 05 '14 at 19:22
  • This seems like a good time to ask about @@. I know that Command@... is the same as Command[...]. I've also recently learned of /@ as notation for MapAt or //@ for MapAll. However, I don't know how to search the documentation for these types of shorthand notations. I'm sure that the @@ shorthand would be in some related page to the one for @, but I'm not sure what the "acting on" action of @ is called, so I can't find the documentation for that. Is there a good way to search for documentation of these shorthands? – Steve Feb 05 '14 at 20:03
  • I'm an idiot, scratch this. For anyone else out there, you can find these shorthands by searching them directly in the documentation site search bar, but you won't be able to find them with a google search such as "mathematica @", etc., which will work for something like "mathematica Refine." I've grown to distrust websites' own search functionality from experience with plenty of bad sites, but the mathematica documentation center is not one of those examples! – Steve Feb 05 '14 at 20:11