Interpolate gridded matrix row-wise

3 visualizzazioni (ultimi 30 giorni)
Tumelo Maja
Tumelo Maja il 18 Ott 2018
Modificato: jonas il 19 Ott 2018
Hi, I have an 860 by 122 matrix producing a plot below.
I want to interpolate over NaN columns but not all the NaNs in the matrix. I tried using interp function but it gives strange value that are unreasonably wrong. See my code below and attached mat file
figure(1)
pcolor(Y1,Ydepth,sst_up_574); shading flat
set(gca,'Ydir','reverse')
set(gca,'xlim',[bin(1) bin(91)],...
'ylim',[0 500],'fontsize',8,'xminortick','on','yminortick','on')
hold on
hc = colorbar;
rmap=jet(20);
newmap=rmap(2:1:20,:);
colormap(newmap);
caxis([4 23]);
grid on;
box on
set(gca,'Fontweight','bold','Fontsize',16);
set(get(hc,'ylabel'),'string','Water Temperature (^oC)','rotation',270,'verticalalignment','bottom');
ylabel('Depth (m)');
xlabel('Date');
title(['Water Temperature: Upstream SG574'], 'fontweight', 'bold','FontSize',18)
  4 Commenti
jonas
jonas il 18 Ott 2018
"I want to interpolate over NaN columns but not all the NaNs in the matrix."
Very ambiguous. Is this what you are looking for? Perhaps you can sketch what NaNs you want to keep?
Tumelo Maja
Tumelo Maja il 19 Ott 2018
Yes. This is what i want.

Accedi per commentare.

Risposta accettata

jonas
jonas il 19 Ott 2018
Modificato: jonas il 19 Ott 2018
Normally you could achieve this easily by interpolating over rows with a function that does not extrapolate by default. I believe interp1 could do this (?). I used a slighly more messy approach, where I drew a polygon around the area of interest, filled all nans, then removed all points outside of the polygon.
Z = sst_up_574;
X = Y1;
Y = Ydepth;
%Scan rows and find first non-NaN
x = nan(size(X,1))
y = nan(size(Y,1))
for j = 1:size(Z,1)
[~,c] = find(~isnan(Z(j,:))==true,1,'first');
if ~isempty(c)
x(j) = X(j,c);
y(j) = Y(j,c);
else
x(j) = max(X(j,:));
y(j) = max(Y(j,:));
end
end
x(isnan(x))=[];
y(isnan(y))=[];
%Build a polygon
x = [x 0];
y = [y min(Y(:))]
%Find points inside of polygon
in = inpolygon(X(:),Y(:),x,y);
%Fill all NaNs
Zq = fillmissing(Z,'linear',2);
%Set values outside of polygon to NaN
Zq(~in) = NaN;
figure(1)
pcolor(X,Y,Zq); shading flat
set(gca,'Ydir','reverse')
set(gca,'xlim',[bin(1) bin(91)],...
'ylim',[0 500],'fontsize',8,'xminortick','on','yminortick','on')
hold on
hc = colorbar;
rmap=jet(20);
newmap=rmap(2:1:20,:);
colormap(newmap);
caxis([4 23]);
grid on;
box on
set(gca,'Fontweight','bold','Fontsize',16);
set(get(hc,'ylabel'),'string','Water Temperature (^oC)','rotation',270,'verticalalignment','bottom');
ylabel('Depth (m)');
xlabel('Date');
title(['Water Temperature: Upstream SG574'], 'fontweight', 'bold','FontSize',18)

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by