Interpolation of (multidimensional) array

조회 수: 11 (최근 30일)
Maron Dolling
Maron Dolling 2021년 9월 15일
댓글: Maron Dolling 2021년 9월 16일
Does anybody know a proper way on how to interpolate an array so, that i can get from something like
[0, 0, 0, 4, 0, 0, 7, 0, 0, 0]
to
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
?
Can one extrapolate this to e.g. a 3D array?
Thanks in advance!

채택된 답변

the cyclist
the cyclist 2021년 9월 15일
So, this is a somewhat strange "interpolation" (and extrapolation) problem. It requires certain assumptions to leap from your data to the result. Are you saying that your data are effectively the following?
y = [4 7];
x = [4 7]; % Inferred because the non-zero values of y occur at the 4th and 7th position
And, are you saying that your data exist at x values of 1:10, because of the length of your original vector?
And, are you saying that you want to linearly extrapolate outside the range 4:7?
If all of that is true, then I guess this does what you want.
data = [0, 0, 0, 4, 0, 0, 7, 0, 0, 0];
x = find(data);
y = data(x);
xq = 1:length(data);
yq = interp1(x,y,xq,'linear','extrap')
yq = 1×10
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000
But this solution hinges on the fact that the element positions (1st element, 2nd element, and so on) are equivalent to the data values, which is really strange. So, I have my doubts that this will achieve what you want in your actual real-life problem.
  댓글 수: 1
Maron Dolling
Maron Dolling 2021년 9월 16일
Sorry, didn't anticipate that my minimal example may be that misunderstandable. But your answer helps a lot, I figured out a good solution.
Just for your understanding, I was trying to calculate a 3D displacement field for an image volume by knowing only two (or a few) distorted surfaces and interpolating linearly along a certain dimension. But I just didn't find a way around a computational heavy loop.
Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by