Tag Info

Hot answers tagged

5

It's a rounding problem. You can see it from: numbers = Rationalize@{0., 0.6, 0.8, 1., 1.2, 1.4, 1.8} midpoints = MovingAverage[numbers, 2] Nearest[numbers, #] & /@ midpoints (* {{0, 3/5}, {3/5, 4/5}, {4/5, 1}, {1, 6/5}, {6/5, 7/5}, {7/5, 9/5}} *) BTW, for long lists and reusable results use Nearest this way: f = Nearest[numbers]; f /@ midpoints


2

Try some thing like this: SetDirectory[dir]; files = FileNames["*.txt"]; Export[newdir <> "\\" <> files[[#]], Cases[Import[files[[#]]], {1, _}][[;; , 2]]] & /@ Range[Length[files]]


1

You can use Select if you are willing to modify your function slightly: Select[lis, (Function[{x, y}, #][2, 2] == 4) &] (*{x + y, x y}*)


1

list = {x - y, x + y, x*y}; Using a different test function, that tests for the equality and accepts the equality value as a parameter: testFun[{a_, b_, c_}] := (# /. {x -> a, y -> b}) == c &; testList = {{2, 2, 4}, {2, 0, 2}}; (* {2, 2, 4} means f[2,2] == 4, and {2, 0, 2} means f[2,0] == 2 *) You can either Map over list, returning the ...


1

Not sure if this would this would achieve what you described (I haven't been using MMA as much as I would like so my answer might be clumsy): Clear[countSameLetters] countSameLetters[list_List, distance_Integer] := Position[ Flatten@Differences@Position[list, #] & /@ CharacterRange["a", "z"], distance] // Length To display each distance with ...


1

comp[a_, b___, c_] := a == c k[list_, dist_] := Count[ListConvolve[SparseArray[{1 -> 1, # -> 1}, #]&[dist+1], list, {-1, 1}, 0, Times, comp], True] list = {d, a, e, a, e, b, e, b, c, d, d}; k[list, #] & /@ Range[Length[list] - 1] (* {1, 4, 0, 1, 0, 0, 0, 0, 1, 1} *)



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