list manipulation - Inserting points along a linear interval between adjacent coordinates in an array
Say I have a list of coordinates in an array
{{0, 0}, {0, 1}, {1, 1}}
I'd like to create a new array where, between each pair of nearest-neighbor coordinates in the previous array, I place $k$ points equispaced on a linear interval between the points. For example, with $k = 3$ we would generate the array:
{{0, 0}, {0, 0.25}, {0, 0.5}, {0, 0.75}, {0, 1},
{0.25, 1}, {0.5, 1}, {0.75, 1}, {1, 1}}
Is there a nice one-liner for doing this?
Answer
edit InterpolationFunction
list = {{0, 0}, {0, 1}, {1, 1}}
k = 3;
r = (k + 1) Length@list - k;
Interpolation[Transpose[{Range[1, r, k + 1], #}], x,InterpolationOrder -> 1
] &/@ Transpose[list] /. {x -> #} & /@ Range[r]
{{0, 0}, {0, 1/4}, {0, 1/2}, {0, 3/4}, {0, 1},
{1/4, 1}, {1/2, 1}, {3/4, 1}, {1, 1}}
old
This works but is suspiciously long:
list = {{0, 0}, {0, 1}, {1, 1}};
k=3;
{list[[1]]}~Join~(
Sequence @@ Table[#1 + i (#2 - #1)/(k + 1), {i, 1, k + 1}] & @@@ Partition[list, 2, 1])
{{0, 0}, {0, 1/4}, {0, 1/2}, {0, 3/4}, {0, 1}, {1/4, 1}, {1/2, 1},
{3/4, 1}, {1, 1}}
I do not like Join
there so this is interesting replacement with Riffle
(the difference is also in iterator range):
Riffle[
Sequence @@ Table[#1 + i (#2 - #1)/(k + 1), {i, 1, k}] & @@@ Partition[list, 2, 1],
list,
{1, -1, k + 1}
]
Comments
Post a Comment