Expand one vector based on another

It was really difficult to come up with a proper title for this question and hence, difficult to google as well. Anyways, my problem is like this:
  1. I have two vectors x and y
  2. x(1), x(end), y(1) and y(end) never change
  3. If I add one or more element, a = [x(1), x(end)] to x: how do I expand y accordingly? Is there a built-in function for this?
Some code to illustrate:
y = [1 2];
y = someFunc([1 2 3], y)
y = [1 1.5 2]
y = [1 2];
y = someFunc([1 2.5 3], y)
y = [1 1.75 2]

1 commentaire

Martin Rindarøy
Martin Rindarøy le 6 Mai 2018
It seems like interp1 actually does exactly what I want. Silly me :)

Connectez-vous pour commenter.

Réponses (1)

Ameer Hamza
Ameer Hamza le 6 Mai 2018
The following function will work for you
function z = someFun(y, y0)
z = y0(1) + (y-min(y))./(max(y)-min(y))*(y0(2)-y0(1));
end
save it in your MATLAB path and call it as follow
y = [1 2];
y = someFun([1 2.5 3], y)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by