Interpolate

6 次查看(过去 30 天)
Mariska Kret
Mariska Kret 2011-4-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

回答(2 个)

Teja Muppirala
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;

Mariska Kret
Mariska Kret 2011-4-30
Thnx for the reply. When I run your script, I get the following errror:
??? Error: File: interpolate_excelfile.m Line: 6 Column: 13 Expression or statement is incorrect--possibly unbalanced (, {, or [.
M
  3 个评论
Walter Roberson
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))
Teja Muppirala
Teja Muppirala 2011-4-30
Ah. Good call! Sometimes you take these things for granted...

请先登录,再进行评论。

类别

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