필터 지우기
필터 지우기

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

조회 수: 1 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by