I'm a MATLAB user trying to get familiar with Mathematica. I have long Mathematica sheet that spits out some matrices at the end after much math.

I want to run a loop over couple of different initial parameters and collect all the different matrices that come out. What is the best way to do this?

In MATLAB I would simply create an .m file function that takes inputs as parameters and the matrices as output and put a loop over said function file. Is there a Mathematica equivalent?



closed as off-topic by Sascha, MarcoB, m_goldberg, happy fish, Öskå Feb 20 '17 at 20:42

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – m_goldberg, Öskå
  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Sascha, MarcoB, happy fish
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ You can define a function for that. Some basics about functions, here and here. $\endgroup$ – Sjoerd C. de Vries May 27 '15 at 20:46
  • 1
    $\begingroup$ One question is, how long is long? If not too long a function works well, as suggested by @SjoerdC.deVries. For large .m files, you can write a short file that calls your long file with given parameters and then collects output from it. $\endgroup$ – bbgodfrey May 27 '15 at 20:50
  • $\begingroup$ It is possible to have your notebook save itself everytime an executed cell gives output. $\endgroup$ – J. M. will be back soon May 27 '15 at 21:13
  • 9
    $\begingroup$ Why do you want to separate each function out into its own file? This is really just an idiosyncrasy (and limitation) of MATLAB. You don't need to do it when you use other languages. $\endgroup$ – Szabolcs May 27 '15 at 21:39
  • 2
    $\begingroup$ "I took a look at how to define functions for Mathematica and it seems more suitable for one, two liner equations rather than an entire model". You can actually write long functions that consist of multiple steps quite easily in Mathematica. The typical way to do this, is with Module: fun[input1_, input2_...] := Module[{localvar1,localvar2,...}, line1; line2; line3;...; output] If you want to be really neat, you put the function definition in it's own package file and then call it from the main notebook where you need it. $\endgroup$ – Sjoerd Smit Feb 20 '17 at 17:02

The paradigm for programming in Mathematica is actually very different from MATLAB. In general, rather than define your function in an m-file, you define your function in the notebook. For example, suppose I had a list of numbers which I wanted to apply some function to:

Edit: It has been pointed out that it is also effective to create a custom package. This is true, and once a function is created this is probably the way to go, I'm referring to a workflow more than proper development.

list = Range[1,100 000]; (*Creates a list of 1,2,...,100 000*)

I'm going to implement this a few ways and point out how long it takes. First, a loop:

AbsoluteTiming[Do[list[[i]] = 2*list[[i]], {i, 1, Length[list]}]][[1]]


For a more sophisticated function, this would be painfully slow. Next, I'm going to define a function, and map it onto the list:

list = Range[1, 100000];
doubleFunction[x_] := 2*x;
AbsoluteTiming[doubleFunction /@ list][[1]]


In the second line I defined a function. The third line used the /@ shortcut, which applies the function preceding it to each element of the list following it. Mathematica automatically threads this over the list so it is much more efficient.

However, we can do even better by defining a pure function:

list = Range[1, 100000];
doubleFunction = 2*# &;
AbsoluteTiming[doubleFunction /@ list][[1]]


The line doubleFunction = 2*# &; creates a function which behaves very similarly to doubleFunction[x_] := 2*x;: the # denotes the argument to the function, and the & tells Mathematica that the expression before it is a pure function. I don't know exactly how/why, but Mathematica 'compiles' (may not be correct word) pure functions so they run even faster.

In fact, we can use pure functions anonymously, and write this whole program in a single line:

list = (2*# &) /@ Range[1,100000];

Or, since Times is listable:

list = 2 * Range[1000000];

Anyways, I highly recommend reading some documentation on programming with Mathematica. I'm very experienced with other languages but transitioning to writing code like this is completely different from anything I've done before.

  • 4
    $\begingroup$ In general, rather than define your function in an m-file, you define your function in the notebook. Actually, I would disagree with this a bit. In general, it would be better to define one's functions in packages--incidentally, also called .m or .wl--and then call the package functions from a notebook. I think notebooks should be used mostly for enabling interactivity, but the bulk of the code should be organised into packages. $\endgroup$ – Shredderroy May 28 '15 at 20:17
  • 3
    $\begingroup$ A couple of points: pure functions do not involve any compilation - they are faster because they do not use the pattern matcher. Also, your last example works because Times is listable, not Range. $\endgroup$ – Simon Woods May 28 '15 at 21:23
  • 3
    $\begingroup$ @SimonWoods maybe poster did not mean to reference it, but I think here we really do see the effect of auto-compilation, which does not occur if a pattern definition is used instead. ("CompileOptions" -> "MapCompileLength" is 100.) $\endgroup$ – Oleksandr R. May 29 '15 at 2:59
  • $\begingroup$ @Oleksandr, ah yes of course. I forgot about auto-compilation. $\endgroup$ – Simon Woods May 29 '15 at 5:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.