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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Matt J
Matt J am 11 Jan. 2023
Bearbeitet: Matt J am 11 Jan. 2023
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 Kommentar
Bruce Elliott
Bruce Elliott am 11 Jan. 2023
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!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by