Interpolate gridded matrix row-wise
조회 수: 1 (최근 30일)
이전 댓글 표시
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
jonas
2018년 10월 18일
"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)
채택된 답변
jonas
2018년 10월 19일
편집: jonas
2018년 10월 19일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Geographic Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!