How to display a matrix with many NaN-values?

4 views (last 30 days)
Hello,
I have many values of a 2D field and their corresponding coordinaes in a given x-y-range. Yet the coordinates do not form a uniformly occupied grid, it is more like a sparse matrix, the x and y values are often unique and partially only conain one single value per column/line.
Nevertheless, I want to plot my data and interpolate over the NaN entries but the common functions like pcolor and colormap only return a blank figure (except from the denser occupied edges as you can see in the figure).
How do I recieve a interpolated field?
data=file(:,1); %the data points
X=unique(file(:,2));
Y=unique(file(:,3)); %the x and y-coordinates
Field=NaN(length(X),length(Y)); %creates an empty grid
for j=1:length(data)
Xj=find(X==file(j,2));
Yj=find(Y==file(j,3));
Field(Xj,Yj)=data(j); %fills in the known values
end
figure()
pcolor(Field); shading interp;
  2 Comments
Jan
Jan on 26 Apr 2022
You want to interpolate over the rectangular grid. This is a good idea. As you can see, pcolor does not do what you want to without this interpolation.
The conversion would be faster with:
X = file(:, 2);
Y = file(:, 3);
Field = NaN(max(Y), max(X));
Field(sub2ind(size(Field), Y, X)) = file(:, 1);

Sign in to comment.

Answers (1)

Timo Conrad
Timo Conrad on 26 Apr 2022
Ok, here is what I got now:
Thank you @Akira Agata, scatteredInterpolant was helpful, indeed.
X=unique(file(:,2));
Y=unique(file(:,3));
Field=NaN(length(X),length(Y));
Xj=find(X==file(j,2));
Yj=find(Y==file(j,3));
for j=1:length(file(:,1))
Xj(j)=find(X==file(j,2));
Yj(j)=find(Y==file(j,3));
Field(Xj(j),Yj(j))=file(j,1);
end
[Xq,Yq]=meshgrid(X,Y);
figure()
F=scatteredInterpolant(file(:,2),file(:,3),file(:,1)) ;
F.Method='natural';
F2=F(Xq,Yq);
pcolor(F2);shading interp;
end
It creates a fully colored map of the interpolated data:
Thank you for your help.
  3 Comments
Timo Conrad
Timo Conrad on 27 Apr 2022
Sure, thank you for the help!
I know some variables defined here are redundant, I just wanted everybody to understand the data.
Nevertheless, I am grateful for any improvements!
The matrix 'file' is attached and consists of column 1: the data, column 2, 3 & 4: the x, y,z-coordinates.
Akira Agata
Akira Agata on 28 Apr 2022
Ah, it's a Karman vortex, isn't it?
Thank you for sharing the data. The following is a simple example:
load('file.mat');
x = file(:,2);
y = file(:,3);
z = file(:,1);
[xg, yg] = meshgrid(...
linspace(min(x), max(x)),...
linspace(min(y), max(y)));
F = scatteredInterpolant(x, y, z, 'natural');
zg = F(xg, yg);
figure
surf(xg,yg,zg,...
'EdgeColor','none')
view(2)
daspect([1 1 1])
box on
colorbar('southoutside')

Sign in to comment.

Categories

Find more on Geographic Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!