How to display a matrix with many NaN-values?
4 views (last 30 days)
Show older comments
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
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);
Answers (1)
Timo Conrad
on 26 Apr 2022
3 Comments
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')
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!