Tag Info

Hot answers tagged

5

assoc= <|"a" -> <| aa-> "asc", bb->"asd", cc->0, ImageType->"asd", dd-> "asd"|>|>; KeyMap[ToString]/@assoc (* <|"a" -> <|"aa" -> "asc", "bb" -> "asd", "cc" -> 0, "ImageType" -> "asd", "dd" -> "asd"|>|> *) Update: but what if I have n levels? I hope there is a better/cleaner way to deal ...


5

Update: ClearAll[imtF] imtF[foo_] := Block[{i = 1}, foo[#, i++] & /@ Transpose@#] & Examples: imtF[#2 (Plus @@ #) &][{{a, b, c}, {e, f, g}}] (* {a + e, 2 (b + f), 3 (c + g)} *) xx = {{a, b, c}, {e, f, g}, {x, y, z}}; imtF[#2 (Plus @@ #) &][xx] (* {a + e + x, 2 (b + f + y), 3 (c + g + z)} *) imtF[Plus @@ Times@## &][xx] (* {a + e + ...


3

Since it would seem that your index values i and j will always be the same you need only to Transpose your input and use MapIndexed: MapIndexed[ #[[1]]*#2[[1]] + #[[2]]*#2[[1]] &, {{a, b, c}, {e, f, g}}\[Transpose] ] {a + e, 2 b + 2 f, 3 c + 3 g} Here #[[1]] is the first element, #[[2]] is the second element, and #2[[1]] is the (universal) ...


3

At face value there is this solution: IndexedMapThread[list1_,list2_] := MapThread[(#1*#3+#2*#4 &),{list1,list2,Range@Length@list1,Range@Length@list2}] IndexedMapThread[{a, b, c}, {d, e, f}] (* {a + d, 2 b + 2 e, 3 c + 3 f} *)


3

Your operation using the v10 operator form for MemberQ, along with Alternatives: uno = RandomSample[Range @ 52] ~Partition~ 13; And @@ MemberQ[1 | 14 | 27 | 40] /@ uno Or using modular arithmetic as already provided by others: x = RandomSample @ Range @ 52; Sign[x ~Mod~ 13] ~Partition~ 4 // Total {12, 11, 13, 12} This is the number of non-ace ...


3

Just for fun: You can count number of "aces" (obviously arbitrary choice) 4 sets of 13 cards: Total /@ Partition[ Boole[Mod[#, 13] == 1] & /@ RandomSample[Range@52, 52], 13] For simulation: tab[n_] := Table[Total /@ Partition[Boole[Mod[#, 13] == 1] & /@ RandomSample[Range@52, 52], 13], {n}]; Now cheating by just testing ...


2

Use . for vector and matrix multiplication. The following works: Map[A.#1 &, b] Map[#1.A &, b] Your definition of A misses a comma by the way.


2

A derivative of István's answer: asc = <|"a" -> <|aa -> "asc" + "zzz", bb -> "asd", cc -> 0, ImageType -> "asd", dd -> "asd"|>|>; AssociateTo[asc, "foo" -> asc]; fn[a_Association] := KeyMap[ToString, a] fn[else_] := else fn //@ asc // InputForm <|"a" -> <|"aa" -> "asc" + "zzz", "bb" -> "asd", "cc" ...


2

Interestingly, not any of the association *Map (KeyMap, AssociationMap, KeyValueMap) functions accept a third argument for level specification. One can use Replace but with an extra Evaluate, as the replacement does not evaluate the KeyMap function: ass = <|a -> <|aa -> "aa", ab -> <|ab1 -> "x", ab2 -> "y"|>|>|>; ...


2

You can replace Key[k] with #[[Key[k]]].


2

You may consider this MapIndexed[Times, #] & /@ {{a, b, c}, {e, f, g}} // Plus @@ # & (* {{a + e}, {2 b + 2 f}, {3 c + 3 g}} *) MapIndexed applies a function (in your example Times to all elements of the list giving part specification (in your example i respectively j) as the second argument. The two resulting lists are then added in the ...


1

Here is definition for indexedMapThread that works for any number of lists so long as they are all equal in length. indexedMapThread[args : {_List ..}] := Module[{sizes = Length /@ args, scalars}, If[Not[Equal @@ sizes], Return[$Failed]]; scalars = Range@sizes[[1]]; Expand @ Flatten @ Thread[{#1 Plus[##2]}&[scalars, Sequence @@ args]]] ...


1

Because in your first case, Map[func,l] will evaluate to verbatim l while building up the replacement rules. When the replacement is then done, the replacement rule used is x[l_] -> l. My guess why Map[func, l] evaluates the way it does is that Map works by "inserting" func into it's second argument at the default mapping level, 1. As there is no such ...



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