Tag Info

New answers tagged

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

Flatten@lst[[Last@Position[Partition[Negative@lst[[All, 2]], 2, 1], {True, False}]]] {87.9368, -1.65742}


2

You don't need to Reverse the list... lst[[Last@Position[lst, {_, x_ /; x < 0}] + 1]] (* {{86.2867, 0.0303858}} *)


1

This also works. First@Take[Reverse[lst], First@Position[Reverse[lst], {_, z_} /; z < 0, {1}, 1] - 1] (* {86.2867, 0.0303858} *)


1

{r, c} = Position[data, FirstCase[Reverse@data, x_ /; x < 0, "", 2]]; data[[r + 1]] To make this more robust, more error checking is needed of course, as the above assumes there is at least one more row after the one found etc... but I am sure you can add these error checking.


2

Assuming that the correct answer is {86.2867, 0.0303858} as noted in @Pickett's comment: ClearAll[f] f = With[{lst = Reverse@#, pos = LengthWhile[Reverse@#, #[[2]] > 0 &]}, If[pos == 0, {}, lst[[pos]]]] &; Example: lst = {{117.638, 0.0295688}, {115.988, -2.20012}, {114.338, -9.85732}, {112.688, -4.56951}, {111.038, -5.66421}, ...


4

Horribly inefficient way to do such things. Use IntegerPartitions: set = Range@100; total = 400 s = Select[Tuples[set, 3], Total[#] == t &]; // Timing // First j = Sort[Join @@ Permutations /@ IntegerPartitions[total, {3}, set]]; // Timing // First s == j (* 12.948083 0. True *) The latter is below timing resolution... For your case, ...


3

If order does not matter you could use IntegerPartition. This yields 15 partitions. ip = IntegerPartitions[15, {3}]; v = Pick[ip, Max@# < 11 & /@ ip] yields: {{10, 4, 1}, {10, 3, 2}, {9, 5, 1}, {9, 4, 2}, {9, 3, 3}, {8, 6, 1}, {8, 5, 2}, {8, 4, 3}, {7, 7, 1}, {7, 6, 2}, {7, 5, 3}, {7, 4, 4}, {6, 6, 3}, {6, 5, 4}, {5, 5, 5}} To count the ...


3

This is answer to your original question, sorting only those tuples that are have total sum of 15. You can have a delayed replacement rule with a condition for the required sum: Tuples[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 3] /. x_List /; Total[x] == 15 :> Sort[x] If you actually meant selecting only those tuples, you can simply use Select: ...



Top 50 recent answers are included