Filtering data from interpolated points on patch

2 visualizzazioni (ultimi 30 giorni)
Hi,
The problem would be:
In the figure below, there are 6 points (xy) and the values (q) are assigned to these points. From these points, 3 triangles, colored by value, are drawn using the PATCH command.
By interpolating along two vertical lines I have determined 10-10 values. However, my problem is that it also interpolates values to places where there is no colored triangle (only white background).
Is there any way to zero out all the interpolated values that do not fall on the colored areas?
(This just demonstrates a simpler scenario, there are where I need to filter out these points for a model with many more triangles and white areas.)
The programme can be found below.
FIGURE:
PROGRAM:
xy = [1 3.1
2.6 2.8
1.3 1.9
2.8 1.5
3.7 2.1
2 0.9];
Connectivity_matrix = [3 2 1
5 2 4
6 4 3];
q = [1 2 3 1 2 3]';
RGB = [0 0 1
0 0.5 1
0 1 1
0 1 0.5
0 1 0
0.5 1 0
1 1 0
1 0.5 0
1 0 0];
I = scatteredInterpolant(xy,q);
[x,y] = meshgrid([1.7; 2.4],linspace(0.8,3.2,10));
v = I(x,y)
figure ()
set(gcf, 'color', 'w')
colormap(RGB);
colorbar('EastOutside')
patch('Faces',Connectivity_matrix,'Vertices', xy,'FaceVertexCData',q,'FaceColor','interp')
axis equal
str = compose('%.3f ',v);
text(x(:),y(:),str(:),'HorizontalAlignment','right','BackgroundColor','w')
line(x(:),y(:),'LineStyle','none','Marker','o','Color',[0.75 0 0.75],'MarkerFaceColor',[0.75 0 0.75])
Do you have any idea how can I solve this problem?
Thank you so much for your helpful comments!

Risposta accettata

Voss
Voss il 11 Mar 2024
xy = [1 3.1
2.6 2.8
1.3 1.9
2.8 1.5
3.7 2.1
2 0.9];
Connectivity_matrix = [3 2 1
5 2 4
6 4 3];
q = [1 2 3 1 2 3]';
RGB = [0 0 1
0 0.5 1
0 1 1
0 1 0.5
0 1 0
0.5 1 0
1 1 0
1 0.5 0
1 0 0];
I = scatteredInterpolant(xy,q);
[x,y] = meshgrid([1.7; 2.4],linspace(0.8,3.2,10));
v = I(x,y)
v = 10×2
3.5931 2.8530 3.2777 2.7888 2.9945 1.8470 2.6885 1.5410 2.5098 1.6863 2.6667 1.8431 2.3388 2.0000 1.9162 2.1457 1.4936 1.8717 1.2417 1.8429
% use inpolygon to say which points are in each triangle:
N_triangles = size(Connectivity_matrix,1);
in_triangle = false(numel(x),N_triangles);
for ii = 1:N_triangles
tri_xy = xy(Connectivity_matrix(ii,:),:);
in_triangle(:,ii) = inpolygon(x(:),y(:),tri_xy(:,1),tri_xy(:,2));
end
% a point is in a triangle if it is in any triangle:
in_triangle = any(in_triangle,2);
% zero out the interpolated values of points not in a triangle:
v(~in_triangle) = 0
v = 10×2
0 0 0 0 2.9945 1.8470 2.6885 1.5410 0 0 0 0 2.3388 0 1.9162 2.1457 1.4936 0 0 0
figure ()
set(gcf, 'color', 'w')
colormap(RGB);
colorbar('EastOutside')
patch('Faces',Connectivity_matrix,'Vertices', xy,'FaceVertexCData',q,'FaceColor','interp')
axis equal
str = compose('%.3f ',v);
% text(x(:),y(:),str(:),'HorizontalAlignment','right','BackgroundColor','w')
% line(x(:),y(:),'LineStyle','none','Marker','o','Color',[0.75 0 0.75],'MarkerFaceColor',[0.75 0 0.75])
% make texts and dots only at points in a triangle:
text(x(in_triangle),y(in_triangle),str(in_triangle),'HorizontalAlignment','right','BackgroundColor','w')
line(x(in_triangle),y(in_triangle),'LineStyle','none','Marker','o','Color',[0.75 0 0.75],'MarkerFaceColor',[0.75 0 0.75])
  4 Commenti

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interpolation in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by