Szabolcs
  • Member for 10 years
  • Last seen this week
Why should I avoid the For loop in Mathematica?
111 votes

If you are new to Mathematica, and were directed to this post, first see if you can use Table to solve your problem. I have often told people, especially beginners, to avoid using For in favour of Do....

View answer
How to efficiently find positions of duplicates?
Accepted answer
96 votes

You can use GatherBy for this. You can map List onto Range[...] first if you wish to have exactly the same output you showed. positionDuplicates[list_] := GatherBy[Range@Length[list], list[[#]] &...

View answer
$\LaTeX$ and Mathematica
92 votes

I would like to make my graphics homogeneous with my (LaTeX) document. That means that I would like to use the same font in the graphics (labels for axis etc) as the main text. I recently wrote a ...

View answer
How to create word clouds?
90 votes

A preview Before I show any code, here's a preview of what is possible with some tweaking: First try Here's a go at implementing Wordle's layout algorithm, described at cormullion's link. First, let'...

View answer
What are some useful, undocumented Mathematica functions?
84 votes

LongestCommonSequencePositions and LongestCommonSubsequencePositions Their use is analogous to LongestCommon(Sub)sequence but they return the position of the first match instead. Update: These are ...

View answer
Struct equivalent in Mathematica?
Accepted answer
81 votes

Update: Mathematica 10 has introduced Association, which can be used as a close equivalent of structs. params = <| "par1" -> 1, "par2" -> 2 |> params["par1"] (* ==> 1 *) In version ...

View answer
How to abort on any message generated?
Accepted answer
80 votes

I found a robust solution described in this MathGroup message by Maxim Rytin: messageHandler = If[Last[#], Abort[]] & Internal`AddHandler["Message", messageHandler] This will abort the ...

View answer
Is there a syntax for single-line comments for notebooks?
Accepted answer
79 votes

There is no way to comment out a single line. Mathematica doesn't really respect lines, it pushes working at the expression level when possible (not at the source text level). Converting cells ...

View answer
Is there any algorithm that runs faster in Mathematica than in C or Fortran?
76 votes

High-level languages, like Mathematica, have a high overhead for executing each command/instruction. However, they also typically include commands/instructions that solve a larger and more complex ...

View answer
Why won't Parallelize speed up my code?
Accepted answer
73 votes

This is a general guide on debugging issues with parallelization performance. 1. Measuring performance The proper way to measure the timing of parallelized calculations is AbsoluteTiming, which ...

View answer
How to simplify writing LibraryLink code?
Accepted answer
67 votes

I wrote a package to automatically generate all the boilerplate needed for LibraryLink and for managed library expressions based on a template that describes a class interface: LTemplate Here's how ...

View answer
Does Mathematica have advanced indexing?
63 votes

Update: This is now available as the BoolEval package. I recommend using L UnitStep[15 - T] for good performance. To answer your question about boolean indexing: When you write L[T > 15] = 0 ...

View answer
Generating stippled (Penrose-style) drawings of surfaces
Accepted answer
63 votes

Here's a try: g3 = Graphics3D[{Gray, Sphere[]}, Lighting -> "Neutral", Boxed -> False] img = ColorConvert[Rasterize[g3, "Image", ImageResolution -> 72], "GrayLevel"] edge = ...

View answer
Calling MATLAB from Mathematica
62 votes

2014-04-12 NOTICE: MATLAB R2014a contains a bug that breaks MATLink on OS X and Linux (Windows is fine). If you use MATLink on OS X or Linux, please consider keeping MATLAB R2013b until R2014b comes ...

View answer
$\LaTeX$ and Mathematica
61 votes

A more detailed guide on this topic is included in the MaTeX documentation, in the "Preparing Figures to Size" tutorial. Exporting graphics with consistent font sizes I'll show you my ...

View answer
Generating evenly spaced points on a curve
61 votes

I'm going to brute force it numerically. First, let's define the function we're interested in: fun = KnotData[{3, 1}, "SpaceCurve"] Imagine that this function fun[t] describes the position of a ...

View answer
How to distribute Mathematica packages as paclets?
Accepted answer
54 votes

The following answer is not complete, but does give one possible solution. There's a lot more to learn about the paclet manager, so please contribute another answer if you can, or correct this answer ...

View answer
How to use $\LaTeX$ for typesetting labels and titles in Mathematica
51 votes

MaTeX is a package made exactly for this purpose. It can automatically compile small LaTeX snippets and import them as Mathematica graphics. It is described in detail in this other Mathematica.SE ...

View answer
How symbol lookup actually works
Accepted answer
48 votes

This behaviour has changed since that book was published. I am writing this additional answer to make it clear how Mathematica 9 searches contexts for symbols and that even the current version 9 ...

View answer
How to make an inkblot?
46 votes

Here's a slow and concave version: blot[smoothness_: 20, points_Integer: 10] := With[ {fun = Exp[-smoothness #.#] &, pts = RandomReal[1, {points, 2}]}, RegionPlot[ Total[fun[# - {x, y}] &...

View answer
Is it possible to speed up ContourPlot on multi-core machines?
Accepted answer
46 votes

I second @Verbeia's suggestion: compute the function on a mesh of points and use ListContourPlot. The disadvantage is that ListContourPlot has no adaptive sampling, so it'd be preferable if we could ...

View answer
How can I define a new symbolic constant like Pi?
45 votes

I was recently reminded that the following "functions" are settable, and I was surprised (even though I've seen this before). So I thought that it is valuable to share this information. There are ...

View answer
List of compilable functions
45 votes

Warning: The SetSystemOptions method to detect failed compilation, described below, is not 100% reliable. Please see the comments (e.g. trC = Compile[{{a, _Integer, 2}}, Tr[a]] won't warn). I assume ...

View answer
Extruding along a path
Accepted answer
45 votes

In Mathematica 7 or 8, you can just use Tube. Please see the docs for many, many examples. Example: Show[ParametricPlot3D[{Cos[x], Sin[x], x/5}, {x, 0, 15}] /. Line -> (Tube[#, 0.2] &), ...

View answer
Drawing Clifford Attractors in Mathematica
44 votes

Parameters: a = -1.24458; b = -1.25191; c = -1.815908; d = -1.90866; Compiled function used for iteration: cf = Compile[{{pt, _Real, 1}}, {Sin[a pt[[2]]] + c Cos[pt[[1]]], Sin[b pt[[1]]] + d ...

View answer
Is it possible to invoke the OEIS from Mathematica?
Accepted answer
44 votes

There is a Mathematica package exactly for this at the OEIS wiki. Somewhat related: there's also a package for formatting data into the OEIS format. WolframAlpha also has some of this information, ...

View answer
Copying one symbol into another
Accepted answer
43 votes

Update It turns out that the correct way is to use ExtendedDefinition, not ExtendedFullDefinition. Please see the answer by @jkuczm for a detailed explanation. This is a simplification of your ...

View answer
A one line proof that one is zero using Mathematica 10
42 votes

The behaviour you see is quite reasonable, given what we can expect from computer algebra systems. It will be very similar in other systems too (and exactly the same in all of Mathematica versions 8, ...

View answer
Constructing a list that includes a leading zero (01,02,03 ... 55, 56, etc.)
Accepted answer
42 votes

I always use IntegerString for this (I also number my files in a similar way): In[1]:= IntegerString[#, 10, 2] & /@ Range[87] Out[1]= {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", ...

View answer
Antialiasing in 3D
Accepted answer
42 votes

This needs specific support from your graphics card. My own graphics card is very old, and does not support it, so the slider does nothing on my machine. But the good news is that there are ...

View answer
1
2 3 4 5
79