interpolating data from surrounding cells

2 visualizzazioni (ultimi 30 giorni)
Roisin Loughnane
Roisin Loughnane il 26 Ott 2017
Modificato: Roisin Loughnane il 26 Ott 2017
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 Commento
Roisin Loughnane
Roisin Loughnane il 26 Ott 2017
Modificato: Roisin Loughnane il 26 Ott 2017
EDIT:
For Question 1,
The following code uses linear indexing to get the mean of the surrounding cells omitting the NaNs using nanmean(), and replaces the NaN cell in question (here index 13) with this value.
neighbors = A(13+offsets) % find neighbours of one index NaN cell, 13
repnan = nanmean(neighbors([1 2 3 4 6 7 8 9])) % get the mean of the surrounding cells
A([13]) = repnan % insert mean into that NaN cell

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Data Preprocessing in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by