I’m using Mathematica and have a set of variables (A,B,C,D,…) with properties A=(blue, big, rounded), B=(red, small, spiky), and so forth. Those properties can be common between variables. What would be the best, general way to find all variables that share a common property (of being, for instance, small)? Thanks.
I’m using Mathematica and have a set of variables (A,B,C,D,…) with properties A=(blue, big,
Share
Here’s a list of possible properties:
And here’s a list of objects with some of those properties
You can find all objects that have the property of, e.g., being “blue” using
SelectThis could be wrapped up into a function. Although how I would write that function would depend on the data structures and usage that you’re planning.
Actually, I just reread you question you have a list of objects with some properties and you want to refer to those objects by name. So you probably want something more like
Above I defined some properties that are associated with certain strings. You don’t have to use strings in the above or below, and you can create a better structure than that if you want. You could also make a constructor to create the above, such a constructor could also check if the list of properties supplied is of the right form – i.e. does not have contradictory properties, are all in a list of known properties etc…
We then define a function to test if an object/string has a certain property associated with it
You might want to return an error or warning message if
listProperties[obj]does not have a definition/rule associated with it.Use
Selectto find all “objects” in a list that have the associated property “blue”:There are other ways (probably better ways) to set up such a data structure. But this is one of the simplest ways in Mathematica.