Tag Info

New answers tagged

2

This answer only returns the period. If you want to extract the repeating substring, just use Take[list, period]. sequencePeriod = Compile[{{l, _Integer, 1}}, With[{n = Length[l]}, Catch[ Do[ If[ Catch[ Do[ Do[ If[l[[j]] != l[[k]], Throw[False]];, {k, i + j, n, i} ];, {j, i} ...


4

2nd list: Transpose[{{a1, b1}, {a2, b2}, {a3, b3}, {a4, b4}}] 1st list can be transformed into the second list and then handled the same way: Transpose[Partition[{a1, b1, a2, b2, a3, b3, a4, b4}, 2]] It can also be done with Part ([[ ]]): l1 = {{a1, b1}, {a2, b2}, {a3, b3}, {a4, b4}}; {l1[[All, 1]], l1[[All, 2]]} l2 = {a1, b1, a2, b2, a3, b3, a4, ...


4

l1 = {a1, b1, a2, b2, a3, b3, a4, b4} ; l2 = {{a1, b1}, {a2, b2}, {a3, b3}, {a4, b4}}; Transpose[ArrayReshape[#, {4, 2}]] &@l1 (* {{a1, a2, a3, a4}, {b1, b2, b3, b4}} *) Transpose[ArrayReshape[#, {4, 2}]] &@l2 (* {{a1, a2, a3, a4}, {b1, b2, b3, b4}} *) Also Flatten[l1][[# ;; ;; 2]] & /@ {1, 2} (* {{a1, a2, a3, a4}, {b1, b2, b3, b4}}*) ...


0

Just for variety: in[lst_, a_, b_] := Min[Sign[(# - a) (b - #)] & /@lst] /. {1 -> True, _ -> False} so, in[Flatten@myList, 0.1, 0.15] yields True and in[Flatten@myList, 0.02, 0.1] yields False. Small modification for closed or half open/closed...


4

Edit: f = Flatten[{##}, {3, 2}] & ref: Flatten command: matrix as second argument Old: f[lists__]:=Transpose[Flatten/@ {lists}]


5

This uses partitioning, with padding if required, to make sublists. f = Module[{b, c = 1}, While[Length[b = Union@Partition[#, c, c, {1, 1}, Take[#, c]]] > 1, c++]; {Length@First@b, First@b}] &; Example f@{73, 7, 4, 73, 7, 4, 73, 7, 4, 73, 7, 4, 73, 7} {3, {73, 7, 4}}


9

ClearAll[len] len[{p__, p__ .., e___}] /; MatchQ[{p}, {e, __}] := Length[{p}] len[p_] := Length[p] len /@ lists (* {2, 3, 14} *)


7

I won't bet my hand for this but seems to be ok: ClearAll[return]; return[x : {0 ..., 1}, list_] := {#, list[[;; #]]} &[Length@x]; return[x_, y_] := {Length@y, y}; sqPeriod[list_] := return[FindLinearRecurrence[list], list] sqPeriod /@ { {19, 6, 19, 6, 19, 6, 19, 6, 19, 6, 19, 6}, {73, 7, 4, 73, 7, 4, 73, 7, 4, 73, 7, 4, 73, 7}, {73, 7, 4, ...


1

I'm not quite sure what you're after yet, but if you're looking to Map a function to a submatrix, try MapAt matrix = Array[# &, {3, 3}] MapAt[E^# &, matrix, {2 ;; 3, 2 ;; 3}] // MatrixForm You could also use ReplacePart ReplacePart[ matrix, {i_?(MemberQ[{2, 3}, #] &), j_?(MemberQ[{2, 3}, #] &)} -> f@matrix[[i, j]] ] // ...


1

You may consider spec = Sequence @@ (Span @@ # & /@ rect); Part[t, spec] or t[[spec]] This would lead to: With[{spec = Sequence @@ (Span @@ # & /@ rect)}, t[[spec]] = Map[f, t[[spec]], {2}]]


1

Just for fun a solution with Riffle: riffle[d_] := Module[{data = Transpose[d]}, Partition[Flatten[Transpose[Rest[Riffle[data, {First[data]}]]]], 2] ]


3

f = Through@{Min, Max}@## == {##2} &; f[myList, .1, .15] (* True *) Update: Operator form of AllTrue: AllTrue[.1 <= #[[1]] <= .15&]@myList (* True *) Update 2: some timings of the methods proposed so far: functions = {"Through@{Min, Max}@## == {##2} &[list, min,max]", "AllTrue[min<=#[[1]]<=max&]@list", ...


7

MovingMap as of 10.1 specifies windows by their absolute length. Since lists are treated as implicit time-series with (zero-based) integer index as time, window size of 2 means that the windowing function sees $\{x_{n-2}, x_{n-1}, x_n\}$. This is the only window specification that admits a clean generalization to irregular time series. Using ...


1

AllTrue[Flatten@myList, .1 < # < .15 &] (* True *) which @kguler finds is the fastest.


1

A few alternatives VectorQ[myList, 0.1 < First@# < 0.15 &] (* True *) And @@ Flatten@IntervalMemberQ[Interval[{0.1, 0.15}], myList] (* True *) And @@ Flatten@Function[x, 0.10 < x < 0.15, Listable]@myList (* True *) And @@ MatchQ[x_ /; 0.1 < x < 0.15] @@@ myList (* True *)


1

I'm a bit late coming in here with this, but let there be a different approach anyway. It so happens, that the permutation you describe is simply Cycles[{Range@20}]: Permute[Range@21,Cycles[{Range@20}]] (* {20, 1, 2, ..., 19, 21} *) With mat = Table[p[i,j],{i,0,20},{j,0,20}] You can try this: Transpose@ MapAt[Permute[#, Cycles[{Range@20}]] &, ...


3

Small example on 5x5 matrix: pp = Table[p[i, j], {i, 5}, {j, 5}] One way: pp[[#, #]] &@Insert[Rest@Range[5], 1, -2] Or another: pp[[;; 4, ;; 4]] = RotateLeft[pp[[;; 4, ;; 4]], {1, 1}];


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


2

You main problem here is that a SymmetrizedArray is not an expression once you have constructed it. It is an atom, a raw object that (although it looks otherwise) cannot be decomposed into smaller expressions AA = SymmetrizedArray[{{1, 2} -> (a^2 - b^2)^3, {1, 3} -> ((a - b) (a + b))^3, {2, 3} -> x}, {3, 3}, Antisymmetric[{1, 2}]]; AtomQ[AA] ...


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


9

This is not a bug. MovingMap underwent a significant design change in 10.1. Please consult the documentation for details on the new specification. For your particular example, you would now do MovingMap[h, {a, b, c, d, e, f}, Quantity[2,"Events"]]


0

lst = {"0/0", "1/25", "1/36", "1/49"}; Temporarily change the definition of Indeterminate to Missing["NotAvailable"]: Block[{Indeterminate = Missing["NotAvailable"]}, Quiet@ToExpression@lst] {Missing["NotAvailable"], 1/25, 1/36, 1/49}


2

Possibly you would be pleased with an alternative approach: lst = {"0/0", "1/25", "1/36", "1/49"}; Quiet @ ToExpression @ lst /. Indeterminate -> Missing["Unavailable"] {Missing["Unavailable"], 1/25, 1/36, 1/49}


2

How about something like this? myfun[x_] := If[x == "0/0", Missing["Unavailable"], ToExpression[x]] Map[myfun, lst] {Missing["Unavailable"], 1/25, 1/36, 1/49}


2

lst = {"1", "2", "3", "4", "5", "6"}; Developer`PartitionMap[StringTrim[ToString@#, "{" | "}"] &, lst, 3] (* {"1, 2, 3", "4, 5, 6"} *)


1

This is very similar to your previous question and so is the solution: lst = {"1", "2", "3", "4", "5", "6"} ToString @ Row[#, ", "] & /@ Partition[lst, 3] {"1, 2, 3", "4, 5, 6"}


1

This gives exactly the output you wrote down: str1 = StringDrop[StringDrop[ToString@Partition[lst, 3][[1]], 1], -1] str2 = StringDrop[StringDrop[ToString@Partition[lst, 3][[2]], 1], -1] Could be made more elegant of course, and more flexible.


0

Here's one approach: sublists = {{1,1,1},{2,2},{3,3,3,3}} list = {4,5,6} Map[Flatten,Transpose@{sublists,list},{-3}] (* inline-edit: please, please, don't use this *) (* {{1,1,1,4},{2,2,5},{3,3,3,3,6}} *) Not too great, especially if the elements are non-atomic expressions. Map[Flatten,Transpose@{sublists,list},{1}] or ...


1

Assuming you always want strings with 3 items: Map[StringJoin @@ Riffle[#, ", "] &, Partition[Map[ToString, lst], 3]]


2

StringTake[ToString@#, {2, -2}] & /@ Partition[lst, 3] {"1, 2, 3", "4, 5, 6"}


3

I would echo's @Kuba comments about most of the examples being for Time related windows, I would also note that there is an explicit example in the help that returns a window size of 3, so it could be an indication that its deliberate. MovingMap[Mean, {x1, x2, x3, x4, x5}, 2] (* {1/3 (x1 + x2 + x3), 1/3 (x2 + x3 + x4), 1/3 (x3 + x4 + x5)} *) There is a ...


1

Done in utterly non-functional style 80's Basic-ish: union[lst1_, lst2_] := Module[{res = {}, lists = Join[lst1, lst2], member}, member[lst_, mem_] := (For[k = 1, k <= Length@lst, k++, If[lst[[k]] === mem, Return[True]]]; False); For[j = 1, j <= Length@lists, j++, If[member[res, lists[[j]]], Continue[], AppendTo[res, lists[[j]]]]]; ...


0

I hope I'm not late to this fun-filled party of disfunctional-programming! unionFor[list1_, list2_] := Module[{lista = {list1[[1]]}, listb = {list2[[1]]}, list, i, j}, For[i = 1, i <= Length[list1], i++, For[j = 1, j <= Length[lista], j++, If[list1[[i]] == lista[[j]], Break[]]; If[j >= Length[lista], AppendTo[lista, list1[[i]]]] ...


4

{#age, #weight} -> #result & /@ ds // Normal {{4, 10} -> 1.4, {4, 8} -> 2.3, {5, 15} -> 3.5, {7, 30} -> 5.1}


8

This should do it: ds[All, {#age, #weight, #gender} -> #result &] // Normal {{4, 10, "M"} -> 1.4, {4, 8, "M"} -> 2.3, {5, 15, "F"} -> 3.5, {7, 30, "F"} -> 5.1}


1

Presumably you are doing this as a programming exercise otherwise you would use Union. To perform a manual union you might consider using a binary search to locate and Insert items in a list. However as Mathematica lists are implemented as arrays this is doomed to have poor computational complexity unless you compile it. A rudimentary example: (* your ...


2

Append has no side effect You may try AppendTo a = {x^2 + y^2 == 1}; AppendTo[a, x + y == 0] Solve[a, {x, y}] resulting in (* {{x -> -(1/Sqrt[2]), y -> 1/Sqrt[2]}, {x -> 1/Sqrt[2], y -> -(1/Sqrt[2])}} *) or consider simply Append[a, x + y == 0] // Solve


3

Instead of using a for loop, you may consider Union straight away: Union[RandomInteger[10, 3], RandomInteger[10, 3]] (*{0, 4, 6, 9, 10} Obviously result will different for each run *)


3

I may have misunderstood but: f[a_, b_, n_] := Module[{lg = Length[a[[1]]], m1, m2, m3}, m1 = UpperTriangularize[ ConstantArray[w[b], {n + lg - 1, n + lg - 1}], lg]; m2 = LowerTriangularize[ ConstantArray[w[Transpose[b]], {n + lg - 1, n + lg - 1}], -lg]; m3 = DiagonalMatrix[ConstantArray[w[a], n]]; Plus @@ (ArrayFlatten /@ ({m1, m2, m3} ...


2

Update: ... because my matrix is (214x216) so it's impossible to assign all these values one by one. (* your 62 2X2 matrices av1 through av62 *) avmat = Array[Subscript[Row[{av, #}], ##2] &, {62, 2, 2}]; sa = SparseArray[{Band[{1, 1}] -> avmat, Band[{1, 3}] -> avmat}]; Dimensions@sa (* {124, 126} *) sa[[;; 20, ;; 20]] // MatrixForm (* ...


1

Terse: a = {1, 17, 2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; ToString @ Row[InputForm /@ a, " "] "1 17 2/3 4/5 9/7 3/7 1/7 1/9"


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


5

Another way: Clear[a, b, bt]; m = 2; n = 3; sp = Normal@SparseArray[{ {i_, j_} /; i == j :> a, {i_, j_} /; j > i :> b, {i_, j_} /; i > j :> bt}, {n*m, n*m}]; And replace the matrix with the numerical values a0 = RandomInteger[10, {m, m}]; b0 = RandomInteger[10, {m, m}]; bT = Transpose[b0]; sp /. {a -> a0, b -> b0, bt ...


5

You can try ArrayFlatten in such cases n = 3; A = Table[Subscript[a, i, j], {i, n}, {j, n}]; MatrixForm[A] B = Table[Subscript[b, i, j], {i, n}, {j, n}]; MatrixForm[B] BT = Transpose[B]; MatrixForm[BT] m = 2; M = Table[Piecewise[{{A, i == j}, {B, i < j}, {BT, i > j}}], {i, m}, {j,m}] // ArrayFlatten; MatrixForm[M]



Top 50 recent answers are included