Tag Info

Hot answers tagged

5

For example, this removes both rows and columns containing 100 m = matrix[[Sequence @@ (Complement[Range@Length@matrix, #] & /@ Transpose@Position[matrix, 100])]]; m // MatrixForm For removing rows containing 100 you could do (among others): matrix /. {___, 100, ___} :> Sequence[] or DeleteCases[matrix, {___, 100, ___}]


5

My favorite being a slight variation of @Lou's own solution Transpose[{data[[;;, 1, 1]], data[[;;, -1, -1]]}] you can also use Transpose@Rest@Extract[data, {{}, {;; , 1, 1}, {;; , -1, -1}}] (* version 9 only -- does not work in version 10 -- thanks: Simon Woods *) or ReplacePart[data, p_ :> {data[[p, 1, 1]], data[[p, -1, -1]]}] or ...


3

My favorite method being the one in @belisarius' answer using Part, or a slight variation of it, (matrix[[##&@@Complement@@@Transpose[{Range@Dimensions@matrix, Transpose@Position[matrix, 100]}]]]), here are a few more, clunkier, alternatives: matrix//MatrixForm pattern = Join @@ ({{#, _}, {_, #2}} & @@@ Position[matrix, 100]); m2 = ...


3

Let's find out if I understand the problem and if this works: fn[nu_][{o_, a_}] := {# - 1, Delete[a, #]} & @ Mod[nu + o, Length@a, 1] NestList[fn[6], {0, Range@15}, 14][[All, 2]] // Column {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} {1,2,3,4,5,7,8,9,10,11,12,13,14,15} {1,2,3,4,5,7,8,9,10,11,13,14,15} {1,2,4,5,7,8,9,10,11,13,14,15} ...


3

You can use the Postion to exact the positions of 100 tagPos=Position[matrix, 100] (*{{3, 2}, {5, 5}, {6, 7}}*) Then using the Last to achieve the column of 100 Rest/@Position[matrix, 100] (*{{2}, {5}, {7}}*) So lastly, Delete res= Delete[Transpose@matrix, Rest /@ tagPos] // Transpose; MatrixForm@res $$ \left( \begin{array}{cccc} 1 & 3 ...


3

I don't think there is any way to do it using just Part or Extract. With these functions you specify the elements you want at level 1, then at level 2, then level 3 etc. At each level you may specify a single element, a list of elements, All or a Span. There is no syntax for specifying different sub-parts for each of the elements selected at a certain level. ...


3

Use Arrow[{##}]& as the function in MapThread. See Function for more.


3

My try {#1[[1]], #2[[-1]]} & @@@ data or data /. {{f_, __}, {__, l_}} -> {f, l}


3

Please look up Part and Span. You can use data[[ ;; ;; n]]


2

You just have to use slightly customized variant of Arrow. startData = {{3.9, 5.2}, {5.5, 6.7}, {8.8, 5.3}, {3.9, 3.6}, {4.3, 3.8}} endData = {{3.0, 6}, {5.3, 7}, {8.5, 6}, {4, 3.6}, {4.2, 4}}; MapThread[Arrow[{#1, #2}] &, {startData, endData}] {Arrow[{{3.9, 5.2}, {3., 6}}], Arrow[{{5.5, 6.7}, {5.3, 7}}], Arrow[{{8.8, 5.3}, {8.5, 6}}], Arrow[{{3.9, ...


2

As well as Part an Span, you could also use Take. data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12}; data[[3 ;; -1 ;; 3]] Take[data, {3, -1, 3}] Both give {7, 5, 1, 12}


2

josephus[nu_, list_] := NestList[Rest@RotateLeft[#, nu - 1] &, list, Length[list] - 1] josephus[6, Range[15]] (* {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5}, {13, 14, 15, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11}, {4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 1, 2}, {11, 13, 14, 15, 1, 2, 4, 5, 7, 8, 9}, {4, ...


2

g[list_, incr_] := Module[{i = 1}, NestWhileList[ Delete[#, i = Mod[i+incr-1, Length@#, 1]] &, list, Length@# > 1 &]] g[Range@15, 6] // Column (*{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15}, {1, 2, 4, 5, 7, 8, 9, 10, 11, ...


2

Update: ClearAll[f2]; Join @@ (With[{k = #}, (Times @@@ {Complement[k, #], #}) & /@ Subsets[k, {2}]] & /@ Tuples[{#1, Times@##} & @@@ #]) &; f2@{{a, a}, {b, b}, {c, c}} f2@{{a, a, a, a}, {b, b, b, b}, {c, c, c, c}} f2@{{a, a}, {b, b}, {c, c}, {d, d}} Original post: ClearAll[f1]; f1 = With[{k = #}, Times @@@ ...


1

The following gets the first two steps: m = {{1, 2}, {0, 2}, {3, 2}, {0, 2}, {0, 2}, {0, 2}, {4, 2}}; v = 1 - Unitize[m[[All, 1]]] (*{0,1,0,1,1,1,0} *) vv = Flatten@(Accumulate /@ Split@v) (* {0,1,0,1,2,3,0} *) Update: ... the last step ClearAll[ff]; ff[0] = 1; ff[n_] := Piecewise[{{ff[n-1] m[[n, 2]], vv[[n]] > 1}, {m[[n, 2]], vv[[n]] == 1}}, m[[n, ...


1

array1 /. Null -> Sequence[] (* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *) Replace[array1, Null :> Sequence[], {2}] (* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *) DeleteCases[array1, Null, {2}] (* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *) SetAttributes[foo, Listable] foo[Null] = Sequence[]; foo[x_] := x foo@array1 (* {{2, 3, 4, 5}, {a, b, ...


1

k[{data_, {}}] := {} k[{data_, rulez_}] := k[{Sow@StepNTimes[data, rulez[[1]], 5], Rest@rulez}] Reap@k[{data, {M1, M2}}] (* {{}, {{StepNTimes[data, M1, 5], StepNTimes[StepNTimes[data, M1, 5], M2, 5]}}} *)


1

Update Update to take into account any "power" and list size as requested. Here you just define what you want : list = {a, b, c}; power=3; Then just run these steps : replist = Table[{# -> #^i}, {i, power}] & /@ list; s1 = Subsets[Rest@list][[1 ;; -2]]; s2 = {Join[{First@list}, #], Complement[Rest@list, #]} & /@ s1; res = Fold[ReplaceAll, ...


1

The lists "a" and "b" are nested lists. With the Flatten option, the nested lists my be flattened and the nesting removed. The answer is, as posted from Öskå, to use the Flatten[]-command before the Transpose[]-command is executed.


1

ClearAll[f, g, q1, q2, q3, d, k1, k2] your list: flist = {f[q1, q2], f[q3, q4], g[q1], g[q2, q3, q4], d[k1 - q1], d[k2 - q2 - q3 - q4]} First set of rules Without repeated rules Make a list of all possible combinations of f[_] and g[_] using Tuples, then Select the ones with IntersectingQ arguments, and change Head from List to Rule using Apply (@@, ...


1

Cases[Transpose @ matrix, a_List /; FreeQ[a, 100]] // Transpose // MatrixForm


1

Your data data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12} Let's say you want every third starting by the second element, that means you want parts {2, 5, 8, 11}. we get those indexes using Range Range[2, Length[data], 3] {2, 5, 8, 11} Now we use this indexes with Part Part[data, Range[2, Length[data], 3]] {5, 9, 2, 13}


1

You can use data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12}; sel = Partition[Range[3, Length@data, 3], 1] Extract[data, sel] But I'm sure there are shorter ways.



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