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 个评论

It seems like interp1 actually does exactly what I want. Silly me :)

请先登录,再进行评论。

回答(1 个)

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)

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by