Extracting a subset from the Faces and Vertices

15 views (last 30 days)
Hello all,
I have a optimimzation problem which I could use some help with. Suppose I have the following data:
% cleaning
close all; clearvars; clc;
% Vertices
ii = 1;
Vertices = zeros(121,2);
for n = 1 : 11
for p = 1 : 11
xCoord = 0.1 * (n-1);
yCoord = 0.1 * (p-1);
Vertices(ii,:) = [xCoord yCoord];
ii = ii + 1;
clear xCoord yCoord
end; clear p
end; clear n ii
% Faces
ii = 1;
Faces = zeros(100,4);
for n = 1 : 10
for p = 1 : 10
Faces(ii,:) = [n+((p-1)*11) n+1+((p-1))*11 n+1+(p*11) n+(p*11)];
ii = ii + 1;
end; clear p
end; clear n ii
% FacesSub
ii = 1;
FacesSub = zeros(16,4);
for n = 4 : 7
for p = 4 : 7
FacesSub(ii,:) = [n+((p-1)*11) n+1+((p-1))*11 n+1+(p*11) n+(p*11)];
ii = ii + 1;
end; clear p
end; clear n ii
% Gives the following surface
figure; axis equal; hold on;
patch('Faces',Faces,'Vertices',Vertices,'FaceAlpha',0.05,'FaceColor','Green')
patch('Faces',FacesSub,'Vertices',Vertices,'FaceAlpha',0.05,'FaceColor','Blue')
% ABOVE CODE IS JUST TO SHOW THE ISSUE. HOWEVER, THE FORMAT IS EQUAL TO THE ACTUAL DATA.
% THIS IS THE INPUT DATA THAT I WORK WITH. I CANNOT EASILY CHANGE THE ABOVE CODE.
As seen from the figure; I have a larger Faces and Vertices surface. This is all fine. I also have a smaller subset of this data; (FacesSub). I can plot this data easily, by including the full set of the Vertices. However, I want to split this as well.
I want to create a VerticesSub which contains only the subset points, and a FacesSub, which contains the faces for only these points:
% Now I can extract the correct indices from the FacesSub
Indices = unique( reshape(FacesSub , [numel(FacesSub),1]) );
% And from this determine the Subset of the Vertices
VerticesSub = Vertices(Indices,:);
Now I have the reduced subset of the Vertices; VerticesSub, and the reduced subset of the Faces; FacesSub. However, they do not work together since the indices inside the FacesSub do not correspond to the VerticesSub. Therefore, using Patch will fail.
I can adjust for this by doing:
% Ugly and slow, but functional solution:
for n = 1 : size(Indices,1)
FacesSub( FacesSub == min(min(FacesSub(FacesSub >= n))) ) = n;
end; clear n
% Check the solution
figure; axis equal;
patch('Faces',FacesSub,'Vertices',VerticesSub,'FaceAlpha',0.05,'FaceColor','Blue')
Which basically loops over all values in the FacesSub, finds the minimum value, and sets it to the correct value. This works, but it is very slow for larger datasets. I am completely stuck in improving this. Can anyone help me with this?
Kind regards, Mark

Accepted Answer

Matt J
Matt J on 26 Jan 2022
Edited: Matt J on 26 Jan 2022
[m,n]=size(FacesSub);
[Indices,~,FacesSub]=unique(FacesSub);
FacesSub=reshape(FacesSub,m,n);
VerticesSub = Vertices(Indices,:);

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!