Tag Info

Hot answers tagged

16

Here is one way: oneseqs[lst_] := Total @ Unitize @ Total[Split[lst], {2}]


14

This s/b quite quick (particularly with long lists): MorphologicalEulerNumber[Image@{list}] And this is even faster... Length[With[{d = Differences@Prepend[list, 0]}, Pick[d, d, 1]]] This seems quite quick: f = Compile[{{z, _Integer, 1}}, Module[{c = True, cnt = 0}, Do[ If[c && x == 1, cnt++; c = False; Continue[]]; If[x == 0, c = ...


10

I suspect you'd be fine with samplelist /. {x__Integer} :> Times@x // Flatten If you always store numbers you want to multiply in lists, and you know them to be integers, this will “apply” Times to all such lists in your data. For more general numbers try something like samplelist /. {x__?NumberQ} :> Times@x // Flatten Note: this way you don't ...


9

Another method: oneseqs[list_] := Count[Append[list, 0] - Prepend[list, 0], 1] Technically, this counts the number of times the sequence shifts from 0 to 1, but that's effectively the same as the number of blocks of 1's. EDIT: Here are some performance figures for the four methods proposed thus far: @Leonard Shifrin: randlist = RandomInteger[1, ...


9

I am late to the party but here is my terse contribution: f1 = Tr @ Split[#][[All, 1]] & This is quite a bit faster than Leonid's oneseqs: x = RandomInteger[1, 500000]; oneseqs[x] // RepeatedTiming f1[x] // RepeatedTiming {0.2992, 125166} {0.0697, 125166} For speed I propose: f2 = Length@# - Tr@# & @ UnitStep @ Differences[# ...


7

If elements are atomic you can use levelspec {-2}: Apply[Times, samplelist, {-2}] {{24}, {1680}} Since Plus[x] and Times[x] each reduce to x you can also use -2 if you not want the extra brackets, as shown by ciao (rasher): Apply[Times, samplelist, -2] {24, 1680} With a dummy head foo to see how this works: Apply[foo, samplelist, -2] ...


6

Some version of the following might be useful: ClearAll[f]; Evaluate@Thread[f[{0, 1, 2}]] := {1, 2, 3}; In this case you could also use Set instead of SetDelayed (:=), because the arguments are "atomic", not patterns. Both = and := hold their first argument unevaluated by default, so that a construct like Thread as I am using it above will only work in ...


5

This s/b quite quick: IntegerDigits[Total@Transpose@(2^Subtract[n, a]), 2, n] N.b. - I changed A to a - it's almost always a bad idea to use uppercase initials for you own symbols... And this is wicked fast: Module[{ca = ConstantArray[0, n w]}, ca[[Flatten[a + Range[0, w n - 1, n]]]] = 1; Partition[ca, n]] Of the answers so far, for n = 20; k = 4; ...


5

list1 = {a, b, c, d}; Rest[FoldList[Times, 1, list1]] Scan[Print, %] a a b a b c a b c d


5

After all these ingenious and interesting solutions only some simple remarks. Jacob Akkerboom's solution shows that totally readable is a subjective concept. It took me some time before I really understood it (but it is very hot here today). In fact, his solution is an ingenious implementation of what could be done with Fold as well, thereby producing ...


4

for any number you can use: count[list_, n_] := Total@Cases[Split[list], {n,n ..} :> 1] count[list1, 1] (*1*) count[list1, 0] (*2*)


4

I am going to demonstrate this with a smaller matrix to enable us to more readily view the results. A0 = Table[Subscript[a, i, j], {i, 8}, {j, 8}]; A0//MatrixForm Step 1. Partition A0 into 4x4 blocks A0byBlock = Partition[A0, {4, 4}]; A0byBlock // MatrixForm Step 2. Map f onto A0 at level 2 A0out = Map[f, A0byBlock, {2}]; A0out // MatrixForm ...


3

My version of Bob Hanlon's comment solution: list1 = {a, b, c, d}; FoldList[Times, list1] % // Column {a, a b, a b c, a b c d} a a b a b c a b c d Reference: Shorter syntax for Fold and FoldList?


3

You can also use Accumulate list1 = {a, b, c, d}; Times @@@ Accumulate[list1] (* or Accumulate[list1] /. Plus -> Times *) {a, a b, a b c, a b c d} ... or ReplaceList: ReplaceList[list1, {x__, ___} :> Times[x]] {a, a b, a b c, a b c d}


3

My comment in Manipulate form: func = {x^2 + x y + y^2}; Manipulate[ D[func, variable], {variable, {x, y}} ] The control that Manipulate uses is the SetterBar[Dynamic[variable], {x, y}] of my comment.


3

Pardon me if I misunderstand but is this all you want? ListPlot[Transpose /@ {{x1, y1}, {x2, y2}}] Or maybe you are looking for the single function form: Transpose[{{x1, y1}, {x2, y2}}, {1, 3, 2}] (* thanks for the correction belisarius *) {{{1, 0}, {3, 2}, {5, 4}, {7, 6}}, {{10, 8}, {12, 10}, {14, 12}, {16, 14}}}


3

You For syntax is just wrong. Try r = Range[10] For[i = 1, i <= 9, i++, k = Complement[r, {i, i + 1}]; Print[k]] and avoid capital letters for your symbol names. K has a build-in meaning Information[K] K is a default generic name for a summation index in a symbolic sum.


3

This works FoldList[Union, Join[List1, List2, List3]]


3

data = {-1011.7, -386.7, 93.3, 345.3, 345.3, 1688.3}; tabul = {{-376.079, -386.7}, {-410.442, -386.7}, {-1028.4, -1011.7}, {-399.11, -386.7}, {-441.029, -386.7}, {325.51, 345.3}, {291.146, 345.3}, {-368.734, -386.7}, {1697.32, 1688.3}, {43.3633, 93.3}}; The strategy is to first group the pairs in tabul by their 2-nd ...


3

Not very fast, but totally readable clusterFu[list_] := Module[ {d = 0, bool = True}, Unevaluated[ Set[bool, True]@If[bool, bool = False; d++] ][[list]]; d ]


3

Your problem is the Set. Set means you're assigning something to a variable (=), x2 Sin[x2] is not a variable. Try Equal[] instead, this is equivalent to ==. Vars = {{(x1) Cos[x2], Sin[x2], 0, (x3) (Sin[x2])}, {(Cos[x2]) (Sin[x4]), (x3) Cos[x4], 1, x1}}; Const = {{1, 0, 0, 1}, {0, 1, 1, 2}}; MapThread[Equal, {Vars, Const}, 2] yields the output: {{x1 ...


3

The introduction of Assocation brings a powerful new way to handle this problem. (* archive data *) ad = {"accept_rate" -> 75, "account_id" -> 395497, "age" -> 41, "badge_counts" -> {"bronze" -> 35, "gold" -> 0, "silver" -> 11}, "creation_date" -> 1326833982, "display_name" -> "Verbeia", "is_employee" -> False, ...


2

Another approach, perhaps only slightly different than @Akater's: samplelist /. v_?VectorQ :> Times @@ v (* {{24}, {1680}} *) One can give VectorQ other arguments that restrict what type of elements the vector v might contain. By default, an element can be anything except a list. For example: {{{x, Exp[x], 3, 4, x Exp[x]}}, {{{5, Plot[Sin[x], {x, ...


2

Here's how I would do it with PaddedForm: Export["filepath.txt", StringJoin[ToString@PaddedForm[#, 6] & /@ {-1, 2, 3, 4, 5}]] This works with negative numbers as well. Will work the same with WriteString. If you want more control over the padding, you can use your own function... custompad[num_, padlength_?IntegerQ] := ...


2

This is very fast : n=20; A={{4, 5, 9, 19}, {3, 7, 8, 11}, {3, 7, 9, 18}, {1, 3, 10, 17}, {5, 6, 10, 19}, {3, 7, 13, 20}, {3, 8, 15, 19}, {2, 7, 15, 20}, {11, 14, 15, 17}, {1, 3, 12, 20}}; f = Compile[{{n, _Integer}, {Ax, _Integer, 1}}, Module[{tab}, tab = Array[0 &, {n}]; (tab[[#]] = 1) & /@ Ax; tab ], RuntimeAttributes ...


2

Untested: SparseArray[Thread[Flatten[Inner[List, Range[w], A, List], 1] -> 1], {w, n}]


2

Using StringCount: StringJoin[ToString /@ list3] // StringCount[#, "1"..] & 3


2

Exactly same approach than @m_goldberg but using GroupBy and MinimalBy : Values@GroupBy[tabul, Last, MinimalBy[#, Abs[Differences[#]] &, Count[data, Last@First@#]] &] {{-376.079, -386.7}}, {{-1028.4, -1011.7}}, {{325.51, 345.3}, {291.146, 345.3}}, {{1697.32, 1688.3}}, {{43.3633, 93.3}}}


2

As mentioned by kglr and ilian in the comments, you can do one of two things. We can define a function that you can map over the function, i.e. #["ParameterTable"] & is the pure function that will accept the Head of a function, and we map this over the list of function as #["ParameterTable"] & /@ Messungfit Alternatively, we can use Through, ...


2

Join is a peculiar function. It interprets its last argument, if and only if that argument is an integer, as a level specification. That is why Join[a, b, Transpose[c], d] complains about level 5 when d = 5. Further, all the arguments except other than that optional last integer must have the same head. So Join[a, d, b, Transpose[c]] won't work either ...



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