Reputation: 35
I have a faces list "faces" in the form n x 3.
Each row contains 3 indices of 3 different rows from a n x 3 vertex list. (So each value is the index to a row which contains 3 coordinates of a vertex.
In my vector "indices" in the form n x 1 I have stored indices of vertices which are of interest for me.
Now I want to see where in my "faces list" these indices from my vector "indices" are contained.
At the moment I'm looping through my faces list to get "vector s" which has the indices to the rows of the "faces list".
To improve the performance I preallocated the "vector s" with zeros.
Is there a faster method to get the same result?
s = zeros(9000000,1);
aa = 0;
for a = 1:size(indices,1)
[i,j] = find (faces == indices(a));
s(aa+1:aa + size(i,1),1) = i;
aa = aa + size(i,1);
end
ss = s(any(s,2),:);
faces = faces(ss(:,1),:);
Thank you for your help. Unfortunately I'm not that proficient with MATLAB and have still trouble vectorizing my code.
Upvotes: 1
Views: 52
Reputation: 16791
The key here is to use ismember
. This gives you a logical array that is 1 wherever an element of the first array is equal to any of the elements in the second, in this case faces
and indices
.
As a toy example,
>> faces = randi(10, 10, 3)
faces =
5 3 9
8 4 4
10 7 10
6 6 3
8 10 1
9 2 2
4 7 9
5 8 3
7 8 4
2 4 9
>> indices = [1, 2, 3].'
indices =
1
2
3
>> s = ismember(faces, indices)
s =
0 1 0
0 0 0
0 0 0
0 0 1
0 0 1
0 1 1
0 0 0
0 0 1
0 0 0
1 0 0
From there, you can continue just as you did before to find rows that have any 1 value in them.
Upvotes: 2