Skip to main content
17 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:...
Ray Shadow's user avatar
  • 7,886
14 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 ...
kglr's user avatar
  • 401k
14 votes
Accepted

How would you explain Nothing[] weirdness?

I think the best way to understand this behavior is with this example, ...
Jason B.'s user avatar
  • 70.2k
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: ...
Mr.Wizard's user avatar
  • 273k
11 votes
Accepted

How to parse JSON

The example JSON string: ...
C. E.'s user avatar
  • 71.2k
11 votes
Accepted

Undefined Indexed Variable

The variable i is a dummy one. The evaluated expression: ...
Alexei Boulbitch's user avatar
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 ...
bill s's user avatar
  • 69.7k
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 ...
Nasser's user avatar
  • 150k
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 ...
xzczd's user avatar
  • 68.4k
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 ...
Hugh's user avatar
  • 16.8k
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. ...
Edward Huff's user avatar
8 votes

How to replace the element in list of list by its ordering index

A method that works for lists with arbitrary structure: ClearAll[ranks] ranks = Internal`CopyListStructure[#, Ordering @ Ordering @ Flatten @ #] &; Examples: <...
kglr's user avatar
  • 401k
7 votes
Accepted

Using sparse matrices when value depends on position

...
kglr's user avatar
  • 401k
7 votes
Accepted

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

Try Pick[foo, mask] (* {a, c}*)
Ulrich Neumann's user avatar
7 votes

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

Graphical explanation of what I understood OP's wants: ...
userrandrand's user avatar
  • 6,027
7 votes
Accepted

How to replace the element in list of list by its ordering index

res={{9, 1, 5}, {3, 6, 12}}; ArrayReshape[InversePermutation[Ordering[Flatten[res]]], Dimensions[res]] (* {{5,1,3},{2,4,6}} *)
user293787's user avatar
7 votes

How to replace the element in list of list by its ordering index

Building off of user293787's answer, but possibly more general because it works for ragged arrays as well: ...
march's user avatar
  • 24.2k
7 votes
Accepted

How to use Position to select based on absence of a pattern?

As far as I understand it, Except is a pattern object and it fits with pattern matching. ...
Syed's user avatar
  • 59.4k
6 votes

Slicing an Association with named key span without normalizing?

From version 10.4 onward, we can define keySpan like this: ...
WReach's user avatar
  • 69.3k
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 ...
user293787's user avatar
5 votes

Function over a list that depends on the index

A couple more options -- lst = {a, b, c, d}; Table[lst[[n]] + n, {n, Length[lst]}] {1 + a, 2 + b, 3 + c, 4 + d} ...
MelaGo's user avatar
  • 9,111
5 votes

How to include index for the coefficients in series or sequence

Try this: Sum[Times[ToExpression[ToString[a] <> ToString[i]], x^i], {i, 1, 5}] (*a1 x + a2 x^2 + a3 x^3 + a4 x^4 + a5 x^5*) Or with Table: ...
E. Chan-López's user avatar
5 votes

How to include index for the coefficients in series or sequence

ClearAll["Global`*"] Programmatically, it is much easier to deal with an indexed variable (e.g., a[3], ...
Bob Hanlon's user avatar
  • 163k
5 votes
Accepted

Substituting the element of a list into a list that contains the index position

coords = Flatten[groupedVerticesRT, 1]; expandedRT = ReplaceAll[i_Integer :> coords[[i]]] @ facesRT Alternatively, you can use ...
kglr's user avatar
  • 401k
4 votes

Unable to use indexed variables with NDEigensystem

Based on Liu's answer I'd like to suggest using: us = Table[Symbol[StringJoin["u", StringPadLeft[ToString[i], 2, "0"]]][t, x, y], {i, n}]; This has the ...
user21's user avatar
  • 40.7k
4 votes

Add Index to All Elements of a List

list = {"a", "b", "c", "d"}; Array["N" <> ToString@# <> " " <> list[[#]] &, Length@list] {"N1 a", "N2 b", "N3 c", "N4 d"}
kglr's user avatar
  • 401k
4 votes

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

Since this question is about performance, I'd like to add that often it's better to work with lists of integers than lists of booleans since lists of integers can be packed. For example, if you want ...
Sjoerd Smit's user avatar
  • 24.8k
4 votes
Accepted

How to get the class of neighbors in a 2D periodic matrix?

How about this? ...
C. E.'s user avatar
  • 71.2k
4 votes
Accepted

Re-defining the built-in `CharacterRange` for a range of 1500 or more

You can use Alphabet + Tuples + StringJoin: ...
kglr's user avatar
  • 401k
4 votes

Solve with a non-numerical-index constant

Clear["Global`*"] Use Format to format output display of variables, e.g., rm and <...
Bob Hanlon's user avatar
  • 163k

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