Interpolate gridded matrix row-wise

2 visualizaciones (últimos 30 días)
Tumelo Maja
Tumelo Maja el 18 de Oct. de 2018
Editada: jonas el 19 de Oct. de 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 comentarios
jonas
jonas el 18 de Oct. de 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 el 19 de Oct. de 2018
Yes. This is what i want.

Iniciar sesión para comentar.

Respuesta aceptada

jonas
jonas el 19 de Oct. de 2018
Editada: jonas el 19 de Oct. de 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)

Más respuestas (0)

Categorías

Más información sobre Geographic Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by