# Tag Info

4

It depends what you want as result. Let me give you two simple examples: f = <|#, "x2" -> #x^2|> &; h = <|"Value" -> #x + #y + #z|> &; Map[f, testPoints] Map[h, testPoints] and you get

0

Here's a generalization of the above to work with arrays (lists) of arbitrary depth. Also avoids checking the element at your specified position (something which may or may not be desired). nearestNZP = Function[{array, i}, MinimalBy[ Flatten[MapIndexed[ If[#1 != 0 && #2 != i, #2, Unevaluated[Sequence[]]] &, array, ...

1

nrstNZP[l_] := With[{nF = Nearest[Flatten@SparseArray[l]["NonzeroPositions"]]}, With[{nrst = nF[#, 2]}, DeleteCases[nrst, #][[1]]] & /@ #] & Example: SeedRandom[1] sel = RandomInteger[{0, 2}, 20] (* {1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 1, 2, 0, 0, 1, 1} *) Flatten[SparseArray[sel]["NonzeroPositions"]] (* {1, 3, 4, 8, 13, 15, 16, 19, ...

1

Try this one: nearestNonNull[lst_, i_] := First@MinimalBy[ Select[MapIndexed[Flatten@{#1 != 0, #2} &, lst], TrueQ@First@# &][[All, 2]], Abs[i - #] &] sel = RandomInteger[{0, 10}, 10^4]; nearestNonNull[sel, 1234];

10

Download a version of this post as a Mathematica notebook by evaluating the following expression in a fresh notebook: Import["http://goo.gl/NaH6rM"]["http://i.stack.imgur.com/Yfan3.png"] Folds over Lists, Lazy Lists (Iteractive) and Tasks (Reactive) We'd like to have a symmetric set of operations over Lists, lazy lists (streams, iterables, or iteractive ...

1

MapThread[Compose, {Array[If[#1 <= #2, f, Identity] &, Dimensions@m], m}, 2]

1

ad[mat_?MatrixQ, func_] := Module[{pos}, pos = Position[UpperTriangularize@ConstantArray[1, Dimensions@mat], 1]; MapAt[f, mat, pos] ] Test n = 4; m = Range[n^2]~Partition~n; ad[m, f] // MatrixForm $\left( \begin{array}{cccc} f[1] & f[2] & f[3] & f[4] \\ 5 & f[6] & f[7] & f[8] \\ 9 & 10 & f[11] & f[12] ... 0 Just wanted to join party but not near computer...will check edit when I get chance ad[m_,f_]:= Module[{n = Length[m[[1]]], mf = Flatten[m], nf}, nf = List /@ Flatten[NestList[n + Rest@# &, Range[n], n - 1]]; Partition[MapAt[f, mf, nf], n]] Here m is square matrix and f function to be applied. For example, MatrixForm[#] -> MatrixForm[ad[#, ... 14 Example square matrix: n = 4; m = Range[n^2] ~Partition~ n; m // MatrixForm$\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \\ \end{array} \right)$Operation: MapAt[f, m, {#, # ;;} & ~Array~ Length @ m] // MatrixForm$\left( ...

0

Building upon @Jinxed's insights, this may be one of the shortest code snippets, though admittedly it isn't efficient code: (f[#] - #) & @ UpperTriangularize@m + m

1

Thanks everyone for contributing interesting suggestions. I thought I'd also attach my own solution: f[m[#1, #2]]& @@@ Select[Tuples[Range @ n, 2], #[[1]] <= #[[2]] &] For n=5 the output is as follows: {f[m[1, 1]], f[m[1, 2]], f[m[1, 3]], f[m[1, 4]], f[m[1, 5]], f[m[2, 2]], f[m[2, 3]], f[m[2, 4]], f[m[2, 5]], f[m[3, 3]], f[m[3, ...

3

In case another way is needed: matrix = Array[m, {5, 5}]; Fold[MapAt[f, #1, {#2, #2 ;;}] &, matrix, Range[5]] (* {{f[m[1, 1]], f[m[1, 2]], f[m[1, 3]], f[m[1, 4]], f[m[1, 5]]}, {m[2, 1], f[m[2, 2]], f[m[2, 3]], f[m[2, 4]], f[m[2, 5]]}, {m[3, 1], m[3, 2], f[m[3, 3]], f[m[3, 4]], f[m[3, 5]]}, {m[4, 1], m[4, 2], m[4, 3], f[m[4, 4]], f[m[4, ...

6

mapAboveDiagonal1 = With[{dim = Dimensions[#2]}, MapAt[#, #2, Join @@ Table[{i, j}, {i, dim[[1]]}, {j, i, dim[[2]]}]]] & or mapAboveDiagonal2 = MapAt[#, #2, SparseArray[UpperTriangularize[ ConstantArray[1, Dimensions[#2]]]]["NonzeroPositions"]]&; mm = Array[m, {5, 5}]; Row[MatrixForm /@ {mm, mapAboveDiagonal1[f, mm]}]

4

Another option is to take advantage of SparseArray index selection: f[x_] := x^2; n = 5; (data = RandomInteger[10, {n, n}]) // MatrixForm And now apply the function f[x] above to only the top triangle SparseArray[{{i_, j_} /; i <= j :> f@data[[i, j]], {i_, j_} /; i > j :> data[[i, j]]}, {n, n}]

0

For the case you gave: f@UpperTriangularize@m

7

I would use MapIndexed, e.g. data = Partition[Range[9], 3]; MapIndexed[If[LessEqual @@ #2, f@#1, #1] &, data, {2}] (* {{f[1], f[2], f[3]}, {4, f[5], f[6]}, {7, 8, f[9]}} *)

9

... why introduce Composition as a new feature? Composition is used to create a new anonymous function that can be used in all the standard ways such as Map and Apply etc. To achieve the same thing without it one needs a Function. Much like operator forms the use of Composition allows one to eliminate extraneous Function constructs which can make code ...

26

Clearly the @ notation is inspired by the usual mathematical notation for function composition. f@g[x] looks very similar to the mathematical notation $(f\circ g)(x)$. But it is important to understand that @ does not denote function composition. In mathematical notation $f\circ g$ is also a function. In Mathematica f@x is simply a different way to ...

5

ClearAll[cyclesF, edgesF] cyclesF = Map[FromCharacterCode, 64 + PermutationCycles[ToCharacterCode@# - 64][[1]], {-1}] &; edgesF = Developer`PartitionMap[DirectedEdge @@ # &, #, 2, 1, {1, 1}] & /@ cyclesF[#] &; str = "AJDKSIRUXBLHWTMCQGZNPYFVOE"; colors = {Red, Green, Blue, Orange, Cyan, Yellow}; vl = cyclesF@str; el = ...

4

Not going to win a beauty contest, but you might get some ideas: string = "AJDKSIRUXBLHWTMCQGZNPYFVOE"; rules[cycle_] := Thread[DirectedEdge[cycle, RotateLeft[cycle]]]; edges = MapIndexed[Style[#1, Thick, ColorData[2][#2[[1]]]] &, rules /@ PermutationCycles[LetterNumber /@ Characters[string], Identity], {2}]; verts = ...

5

s = "AJDKSIRUXBLHWTMCQGZNPYFVOE"; pc = PermutationCycles[ToCharacterCode@s - 64] // First; Graph[Flatten[Thread[# -> RotateRight@#] & /@ pc], VertexLabels -> Table[i -> FromCharacterCode[i + 64], {i, Flatten@pc}], ImagePadding -> 12] Perhaps better: pc = (PermutationCycles[ToCharacterCode@s - 64] // First) /. ...

Top 50 recent answers are included