Tag Info

Hot answers tagged

11

Here is a version which is quite general, and based on Mr.Wizard's dynP function: dynP[l_, p_] := MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p] ClearAll[tdim]; tdim[{dims__Integer}] := Times[dims]; tdim[dims : {__List}] := Total@Map[tdim, dims]; ClearAll[pragged]; pragged[lst_, {}] := lst; pragged[lst_, dims : {__Integer}] ...


10

MapThread[ Partition, {Internal`PartitionRagged[myflatarray, Times @@@ mydimensions], mydimensions[[All, 2]]} ] Internal`PartitionRagged is undocumented. An example of how it works is: Internal`PartitionRagged[{1,2,3,4,5},{3,2}] (* Out: {{1,2,3},{4,5}} *)


10

I think you want Permutations: Permutations[{1, 2, 3}, {2}] {{1, 2}, {1, 3}, {2, 1}, {2, 3}, {3, 1}, {3, 2}}


9

The following should work: lis = {{1, 2, 3, 4, 5, 6}, {1, 9, , 4, 6, 2}, {4, a, 3, 7, 1, 2}, {3.4, 5.2, 6.5, 7.7, 6.1, 2}}; Then: Cases[lis, {__?NumericQ}] {{1, 2, 3, 4, 5, 6}, {3.4, 5.2, 6.5, 7.7, 6.1, 2}} We can also use VectorQ with Select Select[lis, VectorQ[#, NumericQ] &] If you have Version 10, the following works: ...


9

Here is a semi-imperative way: result = Module[{i = 1}, Array[myflatarray[[i++]]&, #]& /@ mydimensions]; result === original (* True *) It uses Array to build up each of the original submatrices, taking successive elements from the flat array.


8

Another functional approach using the Flat attribute: (Credit to Mr Wizard for the clever form of the second line) SetAttributes[f, Flat]; f[1, 0, 0] = Style[#, Red] & /@ f[1, 0, 0]; List @@ f @@ list


7

Using highlight from my answer to Formatting text through pattern matching: ToString[list] /. highlight["1, 0, 0", Style[#, Red] &]


6

You need to select a new data structure. As Leonid Shifrin has written Append[list, element] has a complexity proportional to Length[list] while Append[association, key -> value] is roughly constant time. Another data structure with this same property is linked lists: {newImage, {prevImage, {prevPrevImage,{}}} Any of these two could be used for ...


6

I think that the existing comma-separated-values, fields, containing "new lines" are the offending elements in your file. If you do Length /@ Import["data.csv"] {64, 64, 64, 64, 64, 64, 1, 1, 25, 64, 64, 8, 1, 1, 1, 1, 1, 25, 7, 1, 1, 1, 1, 25, 1, 2, 2, 25, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, ...


6

Modifying halirutan's unflatten and Ray Koopman's copyPartition that appeared in this Q/A unflatten2[a_, dims_] := Module[{i = 1, b = Map[ConstantArray[0, #] &, dims, {-2}]}, Function[Null, a[[i++]], {Listable}][b]]; copyPartition2[a_, dims_] := Module[{i = 0, b = Map[ConstantArray[0, #] &, dims, {-2}]}, ...


5

Using string manipulations seems to speed things up significantly: randomList = RandomInteger[{0, 1}, 1000]; m1 = randomList //. {b___, PatternSequence[1, 0, 0], a___} -> {b, Sequence @@ (Style[#, Red] & /@ {1, 0, 0}), a} // AbsoluteTiming; m2 = StringSplit[StringJoin @@ (ToString /@ randomList), "100" -> Sequence @@ (Style[#, ...


5

list /. (0) :> Style[0, Red]


5

I think those who find this kind problem challenging enough to ask for s solution on this site are likely to be open to a tutorial answer giving a step-by-step exposition. The overall plan is to develop a function that will take one item from the table, compute the date difference for that item, and insert it into the item. With such a function, any data ...


5

My proposal: {##, data[[1, 1]] ~DayCount~ #} & @@@ data {{{2012, 6, 1}, 16, 0}, {{2012, 6, 8}, 14.24, 7}, ..., {{2012, 10, 9}, 4.53, 130}}


5

Here is my contribution: a performance improvement upon WReach's method. Extracting entire rows at a time can be considerably faster with hopefully a minimum of obfuscation. fn[source_, dims_] := Module[{i = 0}, source ~Take~ {i + 1, i += #2} ~Table~ {#} & @@@ dims ] Test: fn[Range@19, {{2, 3}, {4, 1}, {3, 3}}] // Column {{1,2,3},{4,5,6}} ...


5

m = {{6.22926, 6.23254, 6.23128, 0, 6.23453, 6.22548, 6.22938, 6.23443, 0}, {6.22926, 6.23254, 6.23128, , 6.23453, 6.22548, 6.22938, 6.23443,}} Select[m, MatrixQ[{#}, NumericQ] &] {{6.22926, 6.23254, 6.23128, 0, 6.23453, 6.22548, 6.22938, 6.23443, 0}} more alternatives for speed comparisons.


4

Here's how I'd approach it. Basically, use DownValues and DumpSave: frame[1] = (*picture1*); frame[2] = (*picture2*); DumpSave["filepath.mx",frame] With a fresh Kernel: Get[filepath.mx]; Length@DownValues@frame 2 Also, Obligatory warning about DumpSave: It's platform and version specific.


4

MaximalBywas only introduced with V10 For V9 and prior use Last @ SortBy[{{25, 16}, {1, 50}, {2, 8}, {13, 93}, {41, 21}}, Last] {13, 93}


4

Here's one way to resolve the CRLF problem. data = Import[fname, "Text"]; StringReplacePart[data, "\[Wolf]", StringPosition[data, "\n" ~~ DigitCharacter]]; StringReplace[%, "\n" -> ""]; StringReplace[%, "\[Wolf]" -> "\n"]; data2 = ImportString[%, "CSV"]; First, I import the file as a single text string. I notice in the file that all of the ...


4

Another version which will take more general specs, though performance is not great: ragged[l_, dims_] := Module[{x = l}, dims /. {n__Integer} :> ArrayReshape[x, x = Drop[x, 1 n]; {n}]] e.g. ragged[Range[20], {{{2, 3}, {2, 4}}, {2, 3}}] (* { {{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9, 10}, {11, 12, 13, 14}}}, {{15,16, 17}, {18, 19, 20}} } ...


4

DateDifference[First@First@data, #] & /@ (First /@ data) Append[#, #2] & @@@ Thread@{data, %} {0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 130} {{{2012, 6, 1}, 16, 0}, {{2012, 6, 8}, 14.24, 7}, {{2012, 6, 15}, 13.7, 14}, {{2012, 6, 22}, 12.31, 21}, {{2012, 6, 29}, 11.5, 28}, {{2012, 7, 6}, 10.08, 35}, {{2012, ...


3

I keep on thinking how ListCorrelate sounds ideal for this but can't find a way. A functional way (but still slower) would be: g[g[b__], d_] := g[b, d]; g[a___, 1, 0, 0] := Sequence[a, Sequence @@ (Style[#, Red] & /@ {1, 0, 0})] and then using Fold: List @@ Fold[g, First@list, Rest@list] ---EDIT--- which, after Mr Wizard's recommendation, can ...


3

Not sure if this is faster but here it goes: Module[{accumulateLength, subMatrices}, accumulateLength = Accumulate[Times @@@ mydimensions]; subMatrices = MapThread[myflatarray[[#1 ;; #2]] &, {Prepend[Most[accumulateLength] + 1, 1], accumulateLength}]; MapThread[Partition[#1, #2] &, {subMatrices, Last /@ mydimensions}]] The step to generate ...


3

To expand @eldo's answer a bit, you can fully replicate MaximalBy like this: Clear@myMaximalBy; myMaximalBy[e_List, f_, n_: All] := Take[Last@SortBy[GatherBy[e, f], #[[1, -1]] &], n]; Then SeedRandom@1; list1 = {{25, 16}, {1, 50}, {2, 8}, {13, 93}, {41, 21}}; list2 = RandomInteger[10, {100, 2}]; myMaximalBy[list1, Last] {{13, 93}} ...


2

m = {{1, 2, 3}, {, 2, 3}, {1.1, 2, 3}, {1.1, , 3}} DeleteCases[m, Alternatives @@ Select[m, MemberQ[#, _?(Not[NumericQ[#]] &)] &]] {{1, 2, 3}, {1.1, 2, 3}} Edit A timing comparison. First creating a matrix:- m = RandomReal[1, {10000, 10000}]; (* Adding some blanks *) (m[[Sequence @@ #]] = "") & /@ RandomInteger[{1, 10000}, {2000, ...


2

If you're using Mathematica 10, you can also use the new Association feature: frame = Association[]; frame[1] = (*picture1*); frame[2] = (*picture2*); In some ways, associations behave more like lists than downvalues: for example, functions like Map and Select work on them directly. If you assign a different variable to an association, you'll get a copy ...


2

A faster variant of Seismatica's answer: List @@ StringReplace[StringJoin[ToString /@ list], "100" -> {Style[1, Red], Style[0, Red], Style[0, Red]}] /. x_String :> Table[0, {StringLength@x}] // Flatten Here' s a time table running the functions 100 times over a random 0 | 1 list with 1000 members:


2

Maybe this is faster: par[arr_, dim_] := Module[{x = Last /@ dim, y = First /@ dim, l1 = Length @ arr, l2, a}, { a = Take[Partition[arr, y[[1]]], x[[1]]], a = Take[Partition[Take[arr, (l2 = Length @ Flatten @ a) - l1], y[[2]]], x[[2]]], Partition[Take[arr, l2 + Length @ Flatten@a - l1], y[[3]]] }]; par[myflatarray, {{4, 5}, {3, 4}, {2, 3}}] ...


2

{## & @@ #, DateDifference[data[[1, 1]], First@#]} & /@ data (* {{{2012, 6, 1}, 16, 0}, {{2012, 6, 8}, 14.24, 7}, {{2012, 6, 15}, 13.7, 14}, {{2012, 6, 22}, 12.31, 21}, {{2012, 6, 29}, 11.5, 28}, {{2012, 7, 6}, 10.08, 35}, {{2012, 7, 13}, 9.18, 42}, {{2012, 7, 20}, 8.65, 49}, {{2012, 7, 27}, 7.8, 56}, {{2012, 8, 3}, 7.51, 63}, ...


1

data /. {date_: {_, _, _}, v_} :> {date, v, DateDifference[{2012, 6, 1}, date]} {{{2012, 6, 1}, 16, Quantity[0, "Days"]}, {{2012, 6, 8}, 14.24, Quantity[7, "Days"]},... If you are using V10 you need to use QuantityMagnitude to get the number of days as a number. This goes for all answers using DateDifference. data /. {date_: {_, _, _}, v_} :> ...



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