Piecewise linear interpolation - MATLAB Cody - MATLAB Central

Problem 970. Piecewise linear interpolation

Difficulty:Rate

Given an Mx2 vector and a row of M-1 integers, output a two column vector that linearly interpolates Y times between each successive pair of values in X. Any consecutive duplicate points are to be removed.

For example

x = [1 1 ; 3 0 ; 1 1]
y = [5 3]

Your output will be a two column vector that interpolates 5 times between [1 1] and [3 0] (including the boundaries in those five points), and then 3 times between [3 0] and [1 1]. The output should be:

   1   1
   1.5 0.75
   2   0.5
   2.5 0.25
   3   0
   2   0.5
   1   1

There are Y(1)=5 points between the first two coordinate pairs of X [(1,1) and (3,0)], and Y(2)=3 points between the second and third coordinate pairs of X [(3,0) and (2 0.5)]. Notice that there is only one output point for (3,0).

You can assume that there will be no complex numbers, Inf, or NaN in x, and Y>=3 for all values of Y. Good luck!

Solution Stats

39.45% Correct | 60.55% Incorrect
Last Solution submitted on May 05, 2025

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8

Problem Recent Solvers566

Suggested Problems

More from this Author80

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!