1D Interpolation on array without using loops.

조회 수: 11 (최근 30일)
Angela
Angela 2012년 7월 23일
Hello.
I have been pondering this all day.
So, I have an 3D-array, X1, of size 48 x 232 x 61. Y1 is a row vector of size 1 x 232. Each row of X1 corresponds with Y1. I want to interpolate between each row of X1 and Y1 without using any loops. The output is a 48 x 21 x 61 array, Y2_array. With loops, my code is as follows:
% make Y1 array for interpolation
logz = log(10e-8):.10:log(1080);
Y1 = d + exp(logz); % This is the 1 x 232 vector.
Y2 = ones(48,21); % Preallocating Y2 2D-array (size = 48 x 21).
Y2_array = ones(48,21,61); % Preallocating Y2 3D-array (size = 48 x 21 x 61)
% Interpolation
for S = 1:61
for T = 1:48
X1_row = X1(T,:,S);
Y2 = interp1(X1_row, Y1, X2(T,:,S));
% X2 is a 3D-array, size (48 x 21 x 61)
Y2_array(T,:,S) = Y2;
end
end
If I make the inputs of interp1 arrays, Matlab gives me the error message that X and Y need to be vectors.
I know there is a solution to this problem... any suggestion/thoughts about how to execute the interpolation without using loops?
Thanks!!
  댓글 수: 2
Jan
Jan 2012년 7월 23일
What exactly is the problem? As far as I understand your loop method runs successfully. Do you want to run it faster? If so, avoiding the slow INTERP1 is the first thing I'd suggest.
Angela
Angela 2012년 7월 23일
Yeah, I am just trying to make it faster.

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

채택된 답변

Jan
Jan 2012년 7월 23일
편집: Jan 2012년 7월 23일
Try this instead of INTERP1:
function Yi = LeanInterp(X, Y, Xi)
X = X(:);
Xi = Xi(:);
Y = Y(:);
nY = numel(Y);
[dummy, Bin] = histc(Xi, X); %#ok<ASGLU>
H = diff(X);
if Bin(length(Bin)) >= nY
Bin(length(Bin)) = nY - 1;
end
Ti = Bin + (Xi - X(Bin)) ./ H(Bin);
% Interpolation parameters:
Si = Ti - floor(Ti);
Ti = floor(Ti);
% Shift frames on boundary:
d = (Ti == nY);
Ti(d) = Ti(d) - 1;
Si(d) = 1;
% Now interpolate:
Yi = Y(Ti) .* (1 - Si) + Y(Ti + 1) .* Si;
For [1x100] vectors this is about 3 times faster than INTERP1 of Matlab 2009a.
  댓글 수: 1
Angela
Angela 2012년 7월 23일
I will switch to using this method. Thanks!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by