Interpolate
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Teja Muppirala
le 30 Avr 2011
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 commentaires
Mariska Kret
le 30 Avr 2011
3 commentaires
Walter Roberson
le 30 Avr 2011
Mariska, try replacing the ~ on line 6 with an otherwise unused variable name, e.g.,
[valid,unused,values] = find(data(:,col))
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!