interpolating data from surrounding cells
Afficher commentaires plus anciens
I have a large array of ocean climate data that contains many NaN values. I want to interpolate the neighbouring cells of the missing NaN cells, and replace that NaN cell with the mean of it's neighbouring cells that contain actual data.
I think it's best to do this an expanding circular fashion eg. if the nearest surrounding cells of the NaN cell only contain more NaN values, I want to take the second nearest circle of cells and so on, until numeric values are found.
So far, I have been following an answer from here:
A = [1 2 NaN; 3 NaN 5; 6 7 NaN]
A =
1 2 NaN
3 NaN 5
6 7 NaN
A = padarray(A,[1 1],NaN) % pad the edges with NaNs
A =
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1 2 NaN NaN NaN
NaN NaN 3 NaN 5 NaN NaN
NaN NaN 6 7 NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
s=size(A);
N=length(s);
[c1{1:N}]=ndgrid(1:3);
c2(1:N)={2};
offsets=sub2ind(s,c1{:}) - sub2ind(s,c2{:})
offsets =
-8 -1 6
-7 0 7
-6 1 8
L = find(isnan(A)) % find linear indices of NaNs
% find neighbours of one index, 19
neighbors = A(19+offsets)
neighbors =
NaN 3 NaN
NaN 6 7
NaN NaN NaN
Advice on the following would be much appreciated:
1. How do I get the mean of the surrounding cells? EDIT (answer) below.
2. How can I do this to run over the whole matrix?
1 commentaire
Roisin Loughnane
le 26 Oct 2017
Modifié(e) : Roisin Loughnane
le 26 Oct 2017
Réponses (0)
Catégories
En savoir plus sur Calendar dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!