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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Matt J
Matt J il 11 Gen 2023
Modificato: Matt J il 11 Gen 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 Commento
Bruce Elliott
Bruce Elliott il 11 Gen 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!!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by