Hi
There are a lot excellent things in Mata.
But one thing I miss here are more elegant containers with functionality.
Mata is really really powerfull on vectors and matrices, also better than Python I guess even when using Numpy or similar.
And Mata do have Asarray, but it lacks the functionality angle as I see it. And it could have more ease in use.
However there are times when coding where flexible containers with functionality would be nice.
Forgive me for maybe not using the proper words/concept, I'm self-tought.
Some of the toughts I've had I've put into the concept List. It is a prototype but I would like your comments on it.
What it can do is shown here:
It is of course far more interesting when combined with datasets:
So is this just unnecessary? Is something missing? Other comments?
Have fun
The code for doing this on a computer near you is attached here: List.do
There are a lot excellent things in Mata.
But one thing I miss here are more elegant containers with functionality.
Mata is really really powerfull on vectors and matrices, also better than Python I guess even when using Numpy or similar.
And Mata do have Asarray, but it lacks the functionality angle as I see it. And it could have more ease in use.
However there are times when coding where flexible containers with functionality would be nice.
Forgive me for maybe not using the proper words/concept, I'm self-tought.
Some of the toughts I've had I've put into the concept List. It is a prototype but I would like your comments on it.
What it can do is shown here:
Code:
: lst = List() // Instantiate a List object : lst.len() // It is empty 0 : lst.append( (1,4,5,6,3,2,2,3) ) // Append a list of numbers : lst.content() // See the content 1 2 3 4 5 6 7 8 +---------------------------------+ 1 | 1 4 5 6 3 2 2 3 | +---------------------------------+ : : while (lst.has_next()) lst.next() // Loop through the content 1 4 5 6 3 2 2 3 : lst.next() // loop is done : : lst.find(4) // Find the value 4 2 : lst.find(0) == J(1,0,.) // When nothing is found 1 : : // Add one or more elements : lst.append(8) : lst.append((8, 9)) : lst.content() 1 2 3 4 5 6 7 8 9 10 11 +--------------------------------------------------------+ 1 | 1 4 5 6 3 2 2 3 8 8 9 | +--------------------------------------------------------+ : : // Function tochars returns character a for argument 1, b for 2 etc : function tochars(nbr) return(char(nbr+96)) : // Apply function tochars to all values in lst : strofreal(lst.content()) \ lst.apply(&tochars()) 1 2 3 4 5 6 7 8 9 10 11 +--------------------------------------------------------+ 1 | 1 4 5 6 3 2 2 3 8 8 9 | 2 | a d e f c b b c h h i | +--------------------------------------------------------+ : : // A List of characters/strings : lst2 = List() : lst2.append(lst.apply(&tochars())) : : lst2.find("b") // find returns all the positions of b in lst2 1 2 +---------+ 1 | 6 7 | +---------+ : : lst2.unique_values() // Return only unique values 1 2 3 4 5 6 7 8 +---------------------------------+ 1 | a b c d e f h i | +---------------------------------+ : : lst2.frequency(("c", "d")) // The frequency of all (no argument) or a subset of values 1 2 +---------+ 1 | 2 1 | +---------+ : lst2.remove("c") // Remove a single value from lst2 : lst2.frequency(("c", "d")) // Frequency ignores non-present values 1 2 +---------+ 1 | 0 1 | +---------+
Code:
. sysuse auto, clear (1978 Automobile Data) . mata: ------------------------------------------------- mata (type end to exit) ----------------------------- : // Use List on variables : lst = List() : lst.append(st_data(., "rep78")') : lst.unique_values() 1 2 3 4 5 6 +-------------------------+ 1 | 1 2 3 4 5 . | +-------------------------+ : lst.frequency() 1 2 3 4 5 6 +-------------------------------+ 1 | 2 8 30 18 11 5 | +-------------------------------+ : // Set calculations : lst.frequency((4, 5, 6)) 1 2 3 +----------------+ 1 | 18 11 0 | +----------------+ : lst.union_unique((4, 4, 5, 6, 6)) 1 2 3 4 5 6 7 +-----------------------------+ 1 | 1 2 3 4 5 6 . | +-----------------------------+ : lst.intersection_unique((4, 4, 5, 6, 6)) 1 2 +---------+ 1 | 4 5 | +---------+ : lst.less_unique((4, 4, 5, 6, 6)) 1 2 3 4 +-----------------+ 1 | 1 2 3 . | +-----------------+ : end
Have fun
The code for doing this on a computer near you is attached here: List.do
Comment