linear interpolation of matrix.

13 views (last 30 days)
Patrick
Patrick on 18 Nov 2012
Edited: AD on 2 Apr 2018
Hi,
I'm new to matlab so this might be very simple but I would appreciate the help..
I have a matrix of 32 values across and 2209 down. Each line represents one hour of rainfall so these values represent values of rainfall at 1.875min timesteps. I need to use linear interpolation to make this either 2min or 5 min timesteps (a matrix of 30 values across or 12 values across).
for example, trying to convert the 32x2209 into 30x2209.
The new first value would be 0.9375 * the original first value.
The new second value would be (0.0625 * the original first value) + ((0.9375 - 0.0625) * the original second value)
The new third value would be ((2 * 0.0625) * the original second value) + ((0.9375 - (2 * 0.0625)) * the original third value)
... and so on.
Is there an easy way to do this in matlab?
Many thanks in advance to anyone that can help.
Patrick.
  1 Comment
AD
AD on 2 Apr 2018
Edited: AD on 2 Apr 2018
I need to convert the 50000x1001 into 50000x100 matrix using linear interpolation in the time interval of 1 to 20ns. Is there any way ?

Sign in to comment.

Accepted Answer

Teja Muppirala
Teja Muppirala on 19 Nov 2012
This is a method that will preserve area.
%Step 0. Just making some random data to work with...
R = rand(2209,32);
%Step 1. Get the cumulative area
cumulativeArea = [zeros(size(R,1),1) cumsum(R,2)];
%Step 2. Interpolate the accumulated area on the new time intervals
t0 = 0:(1/32):1; %Old
t1 = 0:(1/30):1; %New
cumulativeAreaNew = interp1( t0, cumulativeArea', t1 );
%Step 3. Differentiate the result.
Rnew = diff( cumulativeAreaNew )';
% Just a check:
max(abs(sum(R,2) - sum(Rnew,2))) %Very small
  2 Comments
Patrick
Patrick on 19 Nov 2012
Thanks Teja,
This appears to work perfectly. I don't understand all the commands you have used here but comparing the inputs and the outputs it appears to be doing mathematically what I would hope for!
Patrick.
AD
AD on 2 Apr 2018
Edited: AD on 2 Apr 2018
I need to convert the 50000x1001 into 50000x100 matrix using linear interpolation in the time interval of 1 to 20ns.. Is there any way ?

Sign in to comment.

More Answers (2)

Matt J
Matt J on 18 Nov 2012
Use the INTERP1 command
  2 Comments
Jurgen
Jurgen on 18 Nov 2012
2 minute interpolation:
t = 60/32*(1:32) % base timeline (monotonous increasing)
y = 32 values in row
newT = 2:2:60 % new 2 minute timestep line, assuming end at 60min.
newY = interp1(t,y,newT)
Patrick
Patrick on 19 Nov 2012
Thanks Jurgen,
I am embarrassed to say I explained this really badly/incorrectly in my original question. I am looking to conserve all the mass from my original matrix in a smaller matrix.. decreasing from 32 columns to 30 columns.
therefore the new first value would be original first value + (2/30)*original second value)
the new second value would be (28/30)*original second value + (4/30)*original third value.
etc.
i'm just checking does the interp1 function actually achieve this?
if so, is there a way to adapt your above commands to do all 2209 lines of my matrix at once rather than 1 row?
Thanks in advance,
Patrick.

Sign in to comment.


Ilham Hardy
Ilham Hardy on 19 Nov 2012
A beginner approach,
%1.875 is the default sample rate
n_col_def = (60/1.875);
%row amount
n_row_def = 2209;
%create time vector of original data (start from t = 1.875, hop 1.875,until data end)
time_1 = (60/n_col_def):(60/n_col_def):(60/n_col_def)*n_row_def*n_col_def;
%your matrix name
data_1 = rand(n_col_def,n_row_def);
%transpose the matrix to get correct reshape
data_1_tr = data_1';
%reshape matrix to vector (1*n)
data_1_res = reshape(data_1_tr,1,n_row_def*n_col_def);
% same row amount with 30 columns require lemgth n_time_2 to be specific
n_col_new = (60/2);
n_time_new = n_col_new*n_row_def;
%t_start = 2,sample time 2 minutes . [2,4,6...,]
time_2 = 2:2:(n_time_new*2);
%interpolate the data based on the new time vector (of 5 minutes)
data_2 = interp1(time_1,data_1_res,time_2,'linear');
% reshape vector to matrix
data_2_res = reshape(data_2,n_col_new,[]);
data_2 = data_2_res';

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!