Interpolate gridded matrix row-wise
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I have an 860 by 122 matrix producing a plot below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192022/image.png)
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 Kommentare
jonas
am 18 Okt. 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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193275/image.jpeg)
Akzeptierte Antwort
jonas
am 19 Okt. 2018
Bearbeitet: jonas
am 19 Okt. 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)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!