I’m looking to recreate some of Dr. Wang’s work in Mathematica.
Is there a tool or easy way to convert Matlab scripts to Mathematica? Scripts found here: Princeton Election Consortium
I’m looking to recreate some of Dr. Wang’s work in Mathematica.
Is there a tool or easy way to convert Matlab scripts to Mathematica? Scripts found here: Princeton Election Consortium
Simple: no there isn’t! You would have to ‘translate’ this yourself. Generally you can use a similar coding style as Matlab (using lots of for loops et cetera, which mathematica also supports), but there might be easier ways.
If i look at the code quickly, it looks fairly straight-forward to translate them!
For example the 4th file is easily converted to this:
ClearAll[StateName]
StateName[in_] := Module[{data},
data = <|1 -> "AL", 2 -> "AK", 3 -> "AZ", 4 -> "AR", 5 -> "CA",
6 -> "CO", 7 -> "CT", 8 -> "DC", 9 -> "DE", 10 -> "FL",
11 -> "GA", 12 -> "HI", 13 -> "ID", 14 -> "IL", 15 -> "IN",
16 -> "IA", 17 -> "KS", 18 -> "KY", 19 -> "LA", 20 -> "ME",
21 -> "MD", 22 -> "MA", 23 -> "MI", 24 -> "MN", 25 -> "MS",
26 -> "MO", 27 -> "MT", 28 -> "NE", 29 -> "NV", 30 -> "NH",
31 -> "NJ", 32 -> "NM", 33 -> "NY", 34 -> "NC", 35 -> "ND",
36 -> "OH", 37 -> "OK", 38 -> "OR", 39 -> "PA", 40 -> "RI",
41 -> "SC", 42 -> "SD", 43 -> "TN", 44 -> "TX", 45 -> "UT",
46 -> "VT", 47 -> "VA", 48 -> "WA", 49 -> "WV", 50 -> "WI",
51 -> "WY"|>;
If[KeyExistsQ[data, in], data[in], "XX"]
]
No. It’s not easy.
To port this code, you have to understand what the code is doing in Matlab and then rewrite it in Mathematica. That said, I don’t think it’s too difficult for someone experienced with statistics in Mathematica or Matlab.
There are probably ways of executing code the code from Mathematica. But I don’t think you want that.
I recommend using this code:
With[{abbrevs = EntityClass["AdministrativeDivision", "AllUSStatesPlusDC"][ "StateAbbreviation"]},
AssociationThread[Range[Length@abbrevs] -> abbrevs]
]
In case a state gets added to the union. It’s bad form to have hard values coded in like this.
That’s even better, I didn’t know it was in there! thanks for pointing out!
If you look at the code, remove all the comments, and many of the ‘safety features’ it can be done within the hour… fairly basic code. Also you can use built-in function like Quantile with makes everything a lot easier…