Tag Info

New answers tagged

0

Not sure why you'd need differing variable names, since the index would always be same, but, that aside: imt[f_, l_, v_] := Module[Evaluate@v, MapThread[(v = ConstantArray[#3, Length@v]; f) &, Append[l, Range@Length@l[[1]]]]]; Your example (note last entry is names of variables to be realized): imt[#1*i + #2*j, {{a, b, c}, {e, f, ...


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) ...


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]]] ...


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 + ...


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 ...


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} *)


0

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 ...


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"|>|>|>; ...


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 ...


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.


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 ...


3

Just write simpler code! Here's an example... first, the setup: cards = 52; uno = Partition[RandomSample[Range[cards], cards], cards/4]; aces = 1 + {0, 1, 2, 3} cards/4; (it's a bit easier to test if you set cards = 12). FreeQ[ Intersection[aces, #] & /@ uno, {}] Enjoy.


4

Uuuh, that's like a breeze.... ace= {1, 14, 27, 40}; uno = Partition[RandomSample[Range[52], 52], 13]; Intersection[#, ace] & /@ uno This returns the Aces returned in each hand let's go gambling....hi,hi,hi


1

Here's one way... g[1][x_] := 1; g[2][x_] := x; g[3][x_] := x^2; norm[r_, s_] := Integrate[r[x]*s[x] + r'[x]*s'[x], {x, 0, 1}]; gram=Array[norm[g[#1], g[#2]] &, {3, 3}] or... gram=Table[norm[a, b], {a, {g1, g2, g3}}, {b, {g1, g2, g3}}]; or... gram=Outer[norm, {g1, g2, g3}, {g1, g2, g3}]


2

Use this instead: FindInstance[#, {a, b}]& /@ {a + b > 1 && a + b < 2, a + b > 2 && a + b < 3} or, in fact, this: FindInstance[#, {a, b}]& /@ {{a + b > 1, a + b < 2}, {a + b > 2, a + b < 3}}



Top 50 recent answers are included