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

1 回表示 (過去 30 日間)
Bruce Elliott
Bruce Elliott 2023 年 1 月 11 日
編集済み: Matt J 2023 年 1 月 11 日
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.
  1 件のコメント
Bruce Elliott
Bruce Elliott 2023 年 1 月 11 日
Never mind!! The y values can be a matrix or an array - my mistake!

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by