Interpolate
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I could use some help with a script that I am writing. I have one excel file with 4100 rows and 225 columns consisting of numbers. In case of one or multiple zeros, I want to interpolate these values with the neighbouring numbers. What I did:
function data = interp_datatest(data, method)
orig_valid = find(data(:,:)); %to interpolate all columns and rows
% to test not-0 values
valid_test = orig_valid(1 : end-1); %I cannot use the first and the last datapoint because there are no neighbouring numbers
% to interpolate indices
calc = valid_test(data (valid_test,:)== 0);
% which values are valid?
% first I make a logical array with only zeros
valid = zeros(length(data),1);
% make the not 0-values 1
valid(orig_valid) = 1; % everything with the number '1' is valid and not a zero.
valid(calc) = 0;
valid = logical(valid);
calc = ~valid; % invert valid values for the to be interpolated %values
% interpolate with methode 'spline'
data(calc,:) = interp1(data(valid,1), data(valid,:), data(calc,1), method);
%Loop for all the columns
[rows,columns] = size(data);
for idx = 1:columns
end
There are mistakes in the script. Please help me find them. Another thing I would like to add but I don't know how is that if there are more than 5 consecutive zeros, I do not want to interpolate and leave the zeros.
Thanks a lot for your comments! M
0 个评论
回答(2 个)
Teja Muppirala
2011-4-30
Here's one possible way to do it. I've handled the 5 consecutive zeros thing by using IMCLOSE, a function from the Image Processing Toolbox if you have it.
% This is used to find regions with >= 5 consective zeros
mask = imclose(data,[1;1;1;1;1]);
for col = 1:size(data,2);
%Find the locations and values of nonzero data
[valid,~,values] = find(data(:,col));
%Do not use zeros at the end or the beginning
valid_zeros = intersect(valid(1):valid(end),find(~data(:,col)));
%Call interp1
data(valid_zeros,col) = interp1(valid,values,valid_zeros,method);
end;
data = data.*mask;
0 个评论
Mariska Kret
2011-4-30
3 个评论
Walter Roberson
2011-4-30
Mariska, try replacing the ~ on line 6 with an otherwise unused variable name, e.g.,
[valid,unused,values] = find(data(:,col))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!