Hot answers tagged

46 votes
Accepted

Why do Mathematica list indices start at 1?

I think Leonid's answer deserves to be expanded upon. Most other languages are not symbolic, and thus the "variable name" is not something one needs to keep track of --- ultimately the interpreted or ...
  • 5,484
34 votes
Accepted

What are the requirements for a well behaved indexed variable? Subscript, ToExpression, Downvalue?

General usage Here is what I think Using strings and subsequently ToString - ToExpression just to generate variable names is ...
21 votes
Accepted

Faster position list construction from a "take instructions" list

My answer is based on a modification of a binary heap. Basically the construction looks something like this. We start with a binary tree: Notice that if we label the nodes breadth-first, the labels ...
  • 7,704
17 votes

Faster position list construction from a "take instructions" list

Preface Below, you will find two different solutions. For understanding the problem itself, the first, iterative solution is better suited since it gives insight in how the solution can be found ...
  • 111k
16 votes
Accepted

Map vs. Table for index-specific operations on 2D arrays

Many index-specific operations can be implemented via MapIndexed with a level specificaton. Your Power example can be written as:...
  • 7,686
14 votes
Accepted

How would you explain Nothing[] weirdness?

I think the best way to understand this behavior is with this example, ...
  • 64k
13 votes

Faster position list construction from a "take instructions" list

I have a tree-based method that has the right asymptotics but a very high coefficient. The upshot being, it will not compete with other methods until we get past 10^6 or so in list size. With ...
12 votes

Why do Mathematica list indices start at 1?

Some years ago, a friend of mine was in the supermarket with his son, small kid, who asked him to buy some candy. After some resistance, my friend agreed, but told him that should be just one. In the ...
  • 25.6k
12 votes

Slicing an Association with named key span without normalizing?

I don't know if this is useful to you but it seems a little cleaner than your own code: ...
  • 265k
11 votes
Accepted

Function over a list that depends on the index

MapIndexed: MapIndexed[#2[[1]] + # &, {a, b, c, d}] {1 + a, 2 + b, 3 + c, 4 + d} Also ...
  • 351k
11 votes
Accepted

Undefined Indexed Variable

The variable i is a dummy one. The evaluated expression: ...
10 votes
Accepted

How to parse JSON

The example JSON string: ...
  • 69k
10 votes
Accepted

How to use the value returned from Position as index for another list?

If you put your cursor on the Position command and press F1 for help, you will see the following under Properties and Relations: "Use Extract to extract parts based ...
  • 66k
10 votes
Accepted

Add Index to All Elements of a List

My goal is to add an index to all elements of a list in the form {"a", "b", "c", ... }, so it becomes {"N1 a", "N2 b", "N3 c" ... } may be ...
  • 116k
10 votes

Map vs. Table for index-specific operations on 2D arrays

We don't need to avoid Table in my view. In cases that Table is more straightforward, just use ...
  • 55.2k
9 votes

Basic Search Functionality

...
9 votes
Accepted

Basic Search Functionality

As of Mathematica 11: ...
  • 69k
9 votes

Defining a function of arguments with sub- and superscripts

Following the comments I am encouraging the use of brackets rather than subscripts or superscripts. Here is an example where a function may take a variable with a subscript or a variable without a ...
  • 14.8k
8 votes

What are the requirements for a well behaved indexed variable? Subscript, ToExpression, Downvalue?

Using DownValues enables you to format the display in the subscripted form without using Notation and ...
  • 126k
8 votes

What are the requirements for a well behaved indexed variable? Subscript, ToExpression, Downvalue?

What are the requirements for well behaved variables? Functions are not variables, although in most cases, the kernel treats undefined variables and functions identically. Sometimes it doesn't. ...
7 votes
Accepted

An alternative to subscripts?

An alternative to subscripts as indices... Instead of: {Subscript[x, 1], Subscript[x, 2]} Let's use: x[1], x[2] And this ...
  • 299
7 votes
Accepted

Using sparse matrices when value depends on position

...
  • 351k
7 votes
Accepted

Is there an easy way to index by a binary vector / mask?

Try Pick[foo, mask] (* {a, c}*)
7 votes

Find min and max indices of highest and lowest elements in sorted list

Graphical explanation of what I understood OP's wants: ...
  • 1,131
6 votes

Faster position list construction from a "take instructions" list

Module[{p = Range[Length@#]}, Reap@Fold[(Sow[#[[#2]]]; Drop[#, {#2}]) &, p, #]] &@{1, 1, 2, 1, 1} (* {{}, {{1, 2, 4, 3, 5}}} *)
  • 351k
6 votes

Why does this simple sum function fail to compile?

This seems rather be a error of determining the type of i, j and k than ...
  • 111k
6 votes

Assign zeros to large list of indexed variables

You could use Evaluate[list] = ConstantArray[0, Length[list]] or MapThread[Set, {list, ConstantArray[0, Length[list]]}] to ...
  • 27.2k
6 votes

Slicing an Association with named key span without normalizing?

From version 10.4 onward, we can define keySpan like this: ...
  • 67.5k
6 votes
Accepted

Find min and max indices of highest and lowest elements in sorted list

OP may want to use binary search. There are many posts here on Mathematica SE relating to binary search, including this which also contains compiled code. See also this. Code. To find the highest ...
  • 6,888
5 votes
Accepted

Position not working?

This is indeed somewhat confusing when you are new to Mathematica. In Mathematica, == stands for mathematical equality. Thus ...
  • 228k

Only top scored, non community-wiki answers of a minimum length are eligible