How to interpolate between values in columns of an array without a for loop

3 次查看(过去 30 天)
I have an n-by-1 vector of x values and an n-by-m array of y values. I would like to get m interpolated y values for an arbitrary value in the range of the given x vector.
Can this be done as an array operation? It feels wrong to use a for-loop in Matlab to step through the columns of an array.

采纳的回答

Matt J
Matt J 2023-1-11
编辑:Matt J 2023-1-11
You should use interp1. It's very straightforward.
[m,n]=deal(8,5);
x=(1:n)';
y=reshape(1:m*n,n,m)
y = 5×8
1 6 11 16 21 26 31 36 2 7 12 17 22 27 32 37 3 8 13 18 23 28 33 38 4 9 14 19 24 29 34 39 5 10 15 20 25 30 35 40
yq=interp1(x,y,[1.5;2.5;3.5])
yq = 3×8
1.5000 6.5000 11.5000 16.5000 21.5000 26.5000 31.5000 36.5000 2.5000 7.5000 12.5000 17.5000 22.5000 27.5000 32.5000 37.5000 3.5000 8.5000 13.5000 18.5000 23.5000 28.5000 33.5000 38.5000
  1 个评论
Bruce Elliott
Bruce Elliott 2023-1-11
Whoops! Right after I posted this I realized that the y values can be a matrix or array. I had looked only at the input for x. Sorry!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by