MATLAB Answers

fill missing point in data (x y z 3 columns data)

3 ビュー (過去 30 日間)
h k
h k 2021 年 9 月 13 日
コメント済み: Rena Berman 2021 年 9 月 20 日
I have a data with three columns that have empty spaces inside.
For example, a data has 2500 rows (point) and its length and width are 50 x 60 = 3000
I want to grid this data and put (nan) in place of the blanks
The following plot is drawn with the plot(x,y,'.')
  1 件のコメント
Rena Berman
Rena Berman 2021 年 9 月 20 日
(Answers Dev) Restored edit


回答 (2 件)

Dave B
Dave B 2021 年 9 月 14 日
You can use meshgrid to make x and y grids, and then ismember with the rows flag to find which pairs of x and y are missing:
% Some fake data that simulares your problem
data=[x(:) y(:) rand(numel(x),1)];
% Remove 10 random indices
axis padded
% Make a grid using meshgrid, you'll need at least one point for each grid
% location in either x or y. Otherwise, specify the grid x and y
% explicitly for the first two arguments of meshgrid.
[xi,yi]=meshgrid(unique(data(:,1)), unique(data(:,2)));
notfound = ~ismember([xi(:) yi(:)],data(:,1:2),'rows');
data=[data; xi(notfound) yi(notfound) nan(sum(notfound),1)];
hold on

Chunru 2021 年 9 月 14 日
編集済み: Chunru 2021 年 9 月 14 日
% Generate some data
[x, y] = meshgrid(1:60, 1:50);
x = x(:);
y = y(:);
z = 1./((x-30).^2+(y-20).^2);
% make a square hole
[ix, iy] = meshgrid(20:30, 30:45);
ix = ix(:); iy=iy(:);
idx = sub2ind([50, 60], iy, ix);
x(idx)=[]; y(idx)=[]; z(idx)=[];
% Replace x,y,z with your data
plot(x, y, '*');
% get the grid from data x and y
xg = sort(unique(x));
yg = sort(unique(y));
[xg, yg] = meshgrid(xg, yg);
idx = ~ismember([xg(:) yg(:)], [x y], 'rows');
xg(idx) = nan;
yg(idx) = nan;
imagesc(isnan(xg) & isnan(yg))
axis xy
% Assign z
zg = nan(size(xg));
zg(~idx) = z;
%axis xy
axis xy
Name Size Bytes Class Attributes idx 3000x1 3000 logical ix 176x1 1408 double iy 176x1 1408 double x 2824x1 22592 double xg 50x60 24000 double y 2824x1 22592 double yg 50x60 24000 double z 2824x1 22592 double zg 50x60 24000 double

Community Treasure Hunt

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

Start Hunting!

Translated by