Skip to main content
added 131 characters in body
Source Link
Ali Hashmi
  • 9.1k
  • 4
  • 23
  • 44

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

yet another way using ReplaceRepeated

list1 //. {p___, a : {__Symbol}, b : {__Symbol},c : {__Symbol} ...} :> {p, a, Mean[{a, b}], b, c} (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, ( y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

@Szabolcs and @Bob Hanlon's method (fewer lines, easy to understand, more elegant):

MovingAverage[Riffle[list1, list1], 2] 

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

yet another way using ReplaceRepeated

list1 //. {p___, a : {__Symbol}, b : {__Symbol},c : {__Symbol} ...} :> {p, a, Mean[{a, b}], b, c} (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, ( y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

yet another way using ReplaceRepeated

list1 //. {p___, a : {__Symbol}, b : {__Symbol},c : {__Symbol} ...} :> {p, a, Mean[{a, b}], b, c} (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, ( y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

@Szabolcs and @Bob Hanlon's method (fewer lines, easy to understand, more elegant):

MovingAverage[Riffle[list1, list1], 2] 
added 240 characters in body
Source Link
Ali Hashmi
  • 9.1k
  • 4
  • 23
  • 44

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

yet another way using ReplaceRepeated

list1 //. {p___, a : {__Symbol}, b : {__Symbol},c : {__Symbol} ...} :> {p, a, Mean[{a, b}], b, c} (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, ( y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

yet another way using ReplaceRepeated

list1 //. {p___, a : {__Symbol}, b : {__Symbol},c : {__Symbol} ...} :> {p, a, Mean[{a, b}], b, c} (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, ( y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 
Source Link
Ali Hashmi
  • 9.1k
  • 4
  • 23
  • 44

using Replace and recursion

list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}]]] func[list1]; (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *) 

similar to @Martin Ender but without Riffle

Clear@func; func[list_] := Flatten[#, 1] &@Join[BlockMap[Through[{First, Mean}[#]] &, list, 2, 1], {{Last@list}}]; func[list1] (* {{x1, y1}, {(x1 + x2)/2, (y1 + y2)/2}, {x2, y2}, {(x2 + x3)/2, (y2 + y3)/2}, {x3, y3}} *)