Category Archives: Scientific Computing

Create a symmetric matrix in Mathematica

Here is a quick way to create a symmetric matrix in Mathematica. The code below creates a generic 6×6 matrix (line 1), then mirrors the above-diagonal elements to their below-diagonal counterparts (substitution in lines 3-5). am = Array[Subscript[a,##] &, {6,6}]; am = am /. Flatten[Table[ Subscript[a,i,j] -> Subscript[a,j,i], {i,2,6}, {j,1,i-1}]]
Also posted in Tips & Tricks | Tagged | Leave a comment

Function call counting in Matlab

Here’s a neat way to keep track of the cost, i.e. number of function calls in Matlab: rather than using a counter variable and increment it each time a function is called, use the internal Matlab counters, accessible through the FunctionTable struct array. The loop below is necessary only to get the index of the [...]
Also posted in Tips & Tricks | Tagged | Leave a comment