1D Interpolation on array without using loops.
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Jan
il 23 Lug 2012
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.
Risposta accettata
Jan
il 23 Lug 2012
Modificato: Jan
il 23 Lug 2012
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Interpolation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!