linear interpolation of matrix.
13 views (last 30 days)
Show older comments
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
Accepted Answer
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
More Answers (2)
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';
0 Comments
See Also
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!