Hot answers tagged

9

As Mr. Wizard wisely suggested in his comment, the Suggestion Bar feature is the source of this bug. I have confirmed that turning it off removes the bug. The canonical answer for Suggestion Bar troubles is found here. It shows how to turn it off, under Preferences > Interface.


9

{\[Alpha], \[Chi]} /. list[[All, 2]] or list[[All, 2, All, 2]] If list is very huge, the second method has much higher efficiency.


7

list = {{0.185794, {α -> 5.8794, χ -> 3.14159}}, {0.206365, {α -> 6.07943, χ -> 3.14159}}}; {#2[[1, 2]], #2[[2, 2]]} & @@@ list {{5.8794, 3.14159}, {6.07943, 3.14159}} Also Last /@ Last[#] & /@ list and Extract[list, {All, 2, {1, 2}, 2}]


7

You may use MapIndexed, ReplaceAll, and Nothing. Delete Numbers s1 = {{1, 3, 2}, {1, 3, 2}, {2, 3, 1}, {3, 1}, {2, 3, 1}}; c1 = {{3}, {1, 2}, {3}}; MapIndexed[(s1[[#1]] = (s1[[#1]] /. {First@#2 -> Nothing})) &, c1]; s1 (* {{1, 3}, {1, 3}, {2}, {3, 1}, {2, 3, 1}} *) Delete Sublists s2 = { {{1, 3}, {1, 2}, {3, 2}, {1}, {3}, {2}}, {{1, 3}, {...


5

For the sake of demonstration, let us suppose f[r_Real, p_Real, t_Integer] := r Sin[p t] With[{n = 10}, triples = Transpose[ {RandomReal[{0, 1}, n], RandomReal[{0, 2 Pi}, n], RandomInteger[{1, 10}, n]}]]; Note that this last definition can generate a list of triples of any size by changing the value given to n. Now, N. J. ...


5

Cases[list, {_, {a_ -> x_, b_ -> y_}} :> {x, y}]


5

Table[{a, b}, {a, 16, 65}, {b, 10, Min[16, a - 5 - 1]}] // Flatten[#, 1] &


4

You may use Piecewise. For your For loop function g[x_] = Piecewise[{#, 1 - # <= x <= 2 + #} & /@ Range[4]] Plot[g[x], {x, -3, 6}] For your example function f[x_] = Piecewise[{ {x , 0 <= x <= 1 - 1/5}, {-x + 2 , 1 + 1/5 <= x <= 2}, {1 - 1/5 , True} }] Plot[f[x], {x, 0, 2}] Hope this helps.


4

Given: list = {-3., -2.6, -2.2, -1.8, -1.4, -1., -0.6, -0.2, 0.2, 0.6, 1., 1.4, 1.8, 2.2, 2.6, 3.}; {min, max} = {-1.5, 3}; {pos, non} = {50, 700}; I'd just stick with a direct expression of the requirement: Replace[list, x_ /; min < x < max :> If[Positive[x], pos, non], {1}] (* {-3., -2.6, -2.2, -1.8, 700, 700, 700, 700, 50, 50, 50, 50, 50, 50,...


4

You may use Last, Values, and Composition. Values@*Last /@ list Hope this helps.


3

Do and DeleteCases can do what you want The first one: delete[s_, c_] := Module[{s0 = s}, Do[s0[[c[[i]]]] = DeleteCases[s0[[c[[i]]]], i, 2], {i, Length@c}]; s0 ] The second one delete2[s_, c_] := Module[{s0 = s}, Do[s0[[c[[i]]]] = DeleteCases[s0[[c[[i]]]], {___, i, ___}, 2], {i, Length@c}]; s0 ]


3

In V10, you can use Association to speed this up a bit: bothA = AssociationThread[both[[All, 1]] -> both]; Lookup[bothA, v, {}] (* gives OP's output with {} for missing dates *) Lookup[bothA, v, Nothing] (* omits the {} from output *) Large example -- Update: forgot definition of dates, which had been lost; had to recompute results. dates = ...


3

To do it with Table modify your Tuples example: Flatten[Table[If[a - b > 5, {a, b}, Nothing], {a, Range[16, 65]}, {b, Range[10, 16]}], 1]; If you are not committed to Table another idea is to use Outer Flatten[Outer[If[#1 - #2 > 5, List[#1, #2], Nothing] &, Range[16, 65], Range[10, 16]], 1];


3

If has attribute HoldRest, which you can clear, but I don't recommend that. I think the right solution is to replace a[HoldedArgument] by a[#] &[ToDoBeforeHold], i.e. q[y_] = y; For[i = 1, i <= 4, i++, q[y_] = q[If[1 - i <= x <= 2 + i, #, #2] &[i, y]]]; f[x_] = q[If[7 <= x <= 6, 10 x, x]]; f[x]


2

Your question is ill-posed because you don't tell what you mean by "identify" in "How do I identify which lists have the pair of numbers 20 and 21"? That is, you do not tell us what result you expect from the code that the does the identification. An answer might be as simple as allN = {N1, N2, N3, N4, N5, N6, N7, N8, N9, N10}; Position[allN, {___, 20, 21, ...


2

f[L_List] := ConstantArray[ConstantArray[0, # - 1], # - 5] &[Length[L]] n = 9; ini = ConstantArray[0, n]; For array structure: Fold[Map[f, #, {#2}] &, ini, Range[0, n - 6]] Or if you want a flat result: Nest[Flatten[Map[f, #], 1] &, {ini}, n - 5]


1

Here is a naive solution that can be slow for large lists of points: KFN[list_, k_Integer?Positive] := Module[{kTuples}, kTuples = Subsets[list, {k}]; MaximalBy[kTuples, Total[Flatten[Outer[EuclideanDistance[#1, #2] &, #, #, 1]]] &] ] (Use of Subsets function thanks to N.J. Evans. ) I'm not convinced there is a computationally "efficient"...


1

Either transpose one list against the other or thread over them. Then flatten each sublist. Internally operations are looped over the lists all right, but this is how one normally does things in Mathematica. transposed = Transpose[{list2, list1}]; threaded = Thread[{list2, list1}]; Flatten /@ transposed {{1, a, b}, {2, c, d}, {3, e, f}, {4, g, h}} ...


1

All other introduced symbols starting with N would appear here if Global variables weren't cleared. You should definitely store the lists under one list. vars = Names["Global`N*"]; {"N1", "N10", "N2", "N3", "N4", "N5", "N6", "N7", "N8", "N9"} Select[vars, ContainsAll[ToExpression[#], {20, 21}] &] {"N4", "N6"}


1

What about Table[x[Max[i, j], Min[i, j]], {i, 1, 6}, {j, 1, 6}] // MatrixForm



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