Hot answers tagged

12

MapThread[Append, {listB, listA}, 2] or Join[ listB, Map[List, listA, {-1}], 3 ] {{{n1, p1, a1}, {n2, p2, a2}, {n3, p3, a3}, {n4, p4, a4}, {n5, p5, a5}, {n6, p6, a6}}, {{q1, r1, b1}, {q2, r2, b2}, {q3, r3, b3}, {q4, r4, b4}, {q5, r5, b5}, {q6, r6, b6}}, {{s1, t1, c1}, {s2, t2, c2}, {s3, t3, c3}, {s4, t4, c4}, {s5, t5, c5}, {s6, t6, c6}}, ...


9

Function - just feed it the 5 x 5 array, returns updated array: chg[mat_] := ReplacePart[mat, {x_ /; 2 <= x <= 4, y_ /; 2 <= y <= 4} /; mat[[x, y]] === 0 && mat[[2 x - 3, 2 y - 3]] === mat[[3, 3]] -> mat[[3, 3]]]; Personally, I find even that messy, and prefer (as in this is how ...


8

Martin provided a nice answer with SequenceCases. For Mathematica versions prior to 10.1, depending on what OP wants for lists of the form: list2 = {1, 2, 4, 5, 7, 11, 8, 9, 10, 12, -3, -2, 6, 7, 80}; namely, for lists that have consecutive sequences of same monotonicity, an approach could be: splitMon[list_] := Split[list, sign =.; ...


8

Starting at 10.1, there's a fairly neat solution using SequenceCases: list = {1, 2, 4, 5, 7, 11, 8, 7, 3, 1, -3, -2, 6, 7, 80}; SequenceCases[list, x_ /; Less @@ x || Greater @@ x] (* {{1, 2, 4, 5, 7, 11}, {8, 7, 3, 1, -3}, {-2, 6, 7, 80}} *) This works because SequenceCases defaults to Overlaps -> False, such that e.g. 11 won't appear in two of the ...


6

If you need to get involved with a function of index j, you might find MapIndexed helpful. For example, if you want to drop j th element in the jth sublist, you can do MapIndexed[Drop[#1, #2] &, tab] Here #2 is your {j}, and you can feed it to your function. Be aware of the difference of {j} and j in Drop.


6

ArrayReshape[Flatten[{##}, {2, 3}], Dimensions[#] + {0, 0, 1}] &[listB, listA]


5

Flatten /@ Thread[{#1, #2}] & @@@ Transpose[{listB, listA}] (* {{{n1, p1, a1}, {n2, p2, a2}, {n3, p3, a3}, {n4, p4, a4}, {n5, p5, a5}, {n6, p6, a6}}, {{q1, r1, b1}, {q2, r2, b2}, {q3, r3, b3}, {q4, r4, b4}, {q5, r5, b5}, {q6, r6, b6}}, {{s1, t1, c1}, {s2, t2, c2}, {s3, t3, c3}, {s4, t4, c4}, {s5, t5, c5}, {s6, t6, c6}}, {{u1, v1, d1}, ...


5

Using Ordering: First @ Ordering[Abs[testdat2[[All, 1]] - 0.52], 1] $\ $ 6


4

Generate m tuples of length n from elements of the vector v with an optional offset o to skip initial tuples. mTuples[v_,n_,m_,o_:0]:=Table[v[[IntegerDigits[i,Length[v],n]+1]], {i,o,m-1+o}] Thus mTuples[{0,1},3,2] returns {{0,0,0},{0,0,1}} mTuples[{0,1},3,2,1] returns {{0,0,1},{0,1,0}} and mTuples[{a,b,c},2,7,2] returns ...


4

Here's a very quick-and-dirty, call with player ID, the array, and the move position. Assumes you call with valid move (e.g., not checking for odd row/col or space empty - do before call or add...), it returns the new array with play position and any intervening positions that meet the conditions filled in with player id: doit[player_, grid_, {posx_, ...


4

StringTake and Span would be useful. For example, to get the second characters: StringTake[s, 2 ;; ;; 21] (* "LLFOWJZSPLJJHNHQOQLYSOPWQOSZNTTLTOHETNJOJOODOCJFQOJ" *) To get all of the strings: StringTake[s, Array[# ;; ;; 21&, 21]] Another approach would be a combination of Characters and Part: StringJoin[Characters[s][[2 ;; ;; 21]]] (* ...


4

The first thing you should do is to forget about using For-loops to manipulate lists. If you feel more comfortable with For-loop type thinking then you might use Table[Drop[tab[[i]], -2], {i, Length[data]}] But if you are willing to learn a bit functional coding, I'd recommend Drop[#, -2] & /@ tab


4

If we are to drop the first 2 elements of tab[[1]], the last 2 of tab[[2]], the first 6 of tab[[3]] and the last 2 of tab[[4]] make use of MapThread, e.g. MapThread[Drop, {tab, {2, -2, 6, -2}}] {{0.287282, 0.29287, 0.349432}, {0.335918, 0.320225, 0.306177, 0.294094, 0.28371, 0.274858, 0.267383, 0.260873, 0.255252, 0.247822, 0.245063}, {0.548742, ...


4

Use a Graph with directed edges labels = Thread[ Range[12] -> (Placed[#, Above] & /@ Join[{Subscript[x, 0]}, Range[10], {Subscript[x, f]}])]; Graph[# \[DirectedEdge] # + 1 & /@ Range[11], VertexCoordinates -> d0, VertexLabels -> labels, VertexStyle -> {1 -> Red, 12 -> Blue}] Or, if you need to have it look like the ...


3

This should be much more efficient for anything beyond small lists: (Tr[Tally[#1~Join~#2][[;; Length@#1, 2]]] - Length[#1]) &[listA, listB]


3

r3 = AppendTo[Table[{Graphics[{Text[ Which[i == 1, Subscript[P, 0], i == Length[d0], Subscript[P, f], True, ToString[i - 1]], Offset[{0, 10}, d0[[i]]]]}], Graphics[{PointSize[Large], Which[i == 1, Red], Which[i == Length[d0] - 1, {Point[d0[[i]]], Blue, Point[d0[[i + 1]]]}, True, Point[d0[[i]]]]}], Graphics[{Arrow[d0[[i ;; i + ...


3

Array size/shape agnostic, takes care of edge cases automagically, call with player identifier, position, and current array, returns changed array: fn = With[{cv = #1, cp = #2, cm = #3}, ReplacePart[cm, Select[Position[cm, cv], ChessboardDistance[#, cp] <= 1 &] -> 0]] &; Use example: fn[a, {5, 3}, mat]


3

There are good methods already posted so instead of pragmatic I shall aim for unusual. mkpull[x_] := Module[{i = 1}, x[[i++]] &] pull = mkpull @ Flatten @ listA; PadRight[listB, {Automatic, Automatic, 3}, Unevaluated @ pull[]] Please don't actually use this. ;^)


3

f1 = With[{l = Length@First@Split[Abs@#[[All, 2]], Not[ #1 <= #2] &]}, MapAt[0 &, #, {{l + 1 ;;, 2}}]] &; list = {{10 + I, 3 - 10 I}, {11 - 2 I, 2 - 10 I}, {7 - I, 1 - 9 I}, {3 - 2 I, 1 - 9.1 I}, {1 - I, 1 - 9.3 I}}; f1@list {{10 + I, 3 - 10 I}, {11 - 2 I, 2 - 10 I}, {7 - I, 1 - 9 I}, {3 - 2 I, 0}, {1 - I, ...


3

Here's a solution using SplitBy. It works also for elements that are repeated no more than once, but it can be fixed if there are elements repeated more than once. monotonicSplit[list_] := SplitBy[Transpose[{#, {#1, ##} & @@ Sign@Differences@#}] &@list, Last][[All, All, 1]] list = {1, 2, 4, 5, 6, 6, 7, 3, 1, -3, -2, 6, 7, 80}; monotonicSplit[list] ...


2

The simplest way is to use Thread f[a_, b_] := a + b Thread[f[{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}]] {2, 4, 6, 8, 10} And just to show you a little more Mathematica kung-foo, consider: g[a_, b_] /; a < b := Row[{a, " is less than ", b}] g[a_, b_] /; a > b := Row[{a, " is greater than ", b}] g[a_, b_] /; a == b := Row[{a, " is equal to ", b}] ...


2

A slightly different way using Split Join @@ MapAt[# /. {a_, b_} :> {a, 0} &, Split[list, Abs[#1[[2]]] > Abs[#2[[2]]] &], 2 ;;] (* {{10 + I, 3 - 10 I}, {11 - 2 I, 2 - 10 I}, {7 - I, 1 - 9 I}, {3 - 2 I, 0}, {1 - I, 0}} *)


2

When in doubt, go the direct route list = {{10 + I, 3 - 10 I}, {11 - 2 I, 2 - 10 I}, {7 - I, 1 - 9 I}, {3 - 2 I, 1 - 9.1 I}, {1 - I, 1 - 9.3 I}}; val = Abs@list[[1, 2]]; Do[ If[Abs@list[[n, 2]] <= Abs@val, val = list[[n, 2]], list[[n, 2]] = 0], {n, Length@list}]; list (* {{10 + I, 3 - 10 I}, {11 - 2 I, 2 - 10 I}, {7 - I, 1 - 9 I}, {3 - 2 I, ...


2

Let's say you have this data, just using a random set here for completeness, SeedRandom[55]; (* A set of random 2D coordinates *) list = RandomReal[100, {1000, 2}]; (* attach a set of less-random values as the third coordinate *) list = {#1, #2, #1 + #2 + RandomReal[{-50, 50}]} & @@@ list; You can view the data using a ListDensityPlot or ListPlot, ...


2

Updated to handle OP's MWE. Another approach is to iterate through the master list and locate the positions in the randomly ordered list where positions 3 through 6 occur. I will use the OP example data (see question) for MasterPositionList and list (i.e, the random order list). Locate the rows in the MasterPositionsList where columns three through six ...


2

Edited and updated (There is no requirement for 'Map') The first two tuples: IntegerDigits[#, 2, 3] & @ Range[2] {{0,0,1},{0,1,0}} RotateRight@IntegerDigits[#, 2, 3] &@Range[8] == Tuples[{0, 1}, 3] RotateRight@IntegerDigits[#, 2, 10] &@Range[1024] == Tuples[{0, 1}, 10] IntegerDigits[#, 2, 10] &@Range[1024][[999]] === ...


2

☺ = {## & @@ #, #2} & @@@ # & /@ (# & /@ ({##} &@##)) &; ☺[listB, listA]


2

Here is one way to do it, although I feel like there is probably an even more elegant solution: MapIndexed[ If[ # === clicked && ChessboardDistance[#2, {row, col}] <= 1, 0, # ] &, board, {2} ] E.g. with row = col = 4 on your above example you'd get: {{0, 0, 0, 0, 0, 0}, {0, a, a, b, 0, 0}, {0, a, 0, b, 0, 0}, ...


2

Based on assuming the other answers are correct (since you appear to be unwilling or unable to clarify what correct output is), the following produces the same result but is vastly faster (orders of magnitude) for large lists: Join @@ (GatherBy[MasterPositionsList~Join~list, N@#[[3 ;; 6]] &][[;; Length@MasterPositionsList, 2 ;;]]) Depending on the ...


2

sortedlist = SortBy[list, Position[N@MasterPositionsList[[All, 3 ;; 6]], N@#[[3 ;; 6]]] &]; Note the use of N@ to numericalize the key columns (columns 3 to 5) in both MasterPositionsList and list. Alternatively, using @Jack's approach in a slightly different way: masPosInList = Flatten[Map[Position[N@list[[All, 3 ;; 6]], N@#] &, ...



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