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!

Elmar Zander
on 23 Mar 2017

It seems someone messed up the test code: the defintion of x for the first test is now in the comment line and its impossible now to solve the problem (except cheating...)

1 Comment

Jan Orwat
on 8 Nov 2016

This solution shows a little bit different approach to the problem. It leads to nice, short code.

2 Comments

Alfonso Nieto-Castanon
on 22 Jan 2013

There are some rounding issues with the test suite. assert(isequal(a,b)) where a and b are doubles can be problematic. I would suggest using assert(all(all(abs(a-b)<1e-10))) or something similar instead

James
on 23 Jan 2013

I picked out the numbers in the test cases to hopefully avoid doing that. Looks like your solution (which is quite impressive, btw) runs into the issues I was hoping to avoid.
I'll change the assert condition in a bit.

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
1 player likes this problem

1 player likes this problem