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:...
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
...
14
votes
Accepted
How would you explain Nothing[] weirdness?
I think the best way to understand this behavior is with this example,
...
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:
...
11
votes
Accepted
11
votes
Accepted
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 ...
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
...
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 ...
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 ...
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. ...
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:
<...
7
votes
Accepted
7
votes
Accepted
7
votes
Find min and max indices of highest and lowest elements in sorted list
Graphical explanation of what I understood OP's wants:
...
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}} *)
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:
...
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.
...
6
votes
Slicing an Association with named key span without normalizing?
From version 10.4 onward, we can define keySpan like this:
...
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 ...
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}
...
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:
...
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], ...
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 ...
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 ...
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"}
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 ...
4
votes
Accepted
4
votes
Accepted
Re-defining the built-in `CharacterRange` for a range of 1500 or more
You can use Alphabet + Tuples + StringJoin:
...
4
votes
Solve with a non-numerical-index constant
Clear["Global`*"]
Use Format to format output display of variables, e.g., rm and <...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
index × 103list-manipulation × 40
summation × 18
matrix × 12
indexed-variable × 12
tensors × 7
functions × 6
core-language × 6
plotting × 5
map × 4
variable × 4
subscript × 4
indexing × 4
equation-solving × 3
performance-tuning × 3
replacement × 3
sorting × 3
sparse-arrays × 3
differential-equations × 2
function-construction × 2
mathematical-optimization × 2
expression-manipulation × 2
associations × 2
difference-equations × 2
assignment × 2