How can I delete the elements of a matrix using the indexes of a logical array?

8 次查看(过去 30 天)
Hello everyone! I explain my problem. I am a beginner and hope to be clear.
I have a logical array [1701x1], each element of which refers to a vertex of a mesh.
I also have a [3224x3] array called "faces": every row shows 3 vertices. Every triplet of vertices defines a triangular face. Vertices are numbered from 1 to 1701. So, for example, I can have the row [26 27 28] that refers to the face with 26th, 27th and 28th vertices.
Now, I have to delete some of these faces considering the "values" true or false in the logical array.
In other words, for example, if the 100th element of the logical array is 0 (false), I want the 100th vertex in "faces" to be deleted, in every row it is present.
So I'm going to write the following double for loop, but I am not able to specify the right if statement. That's why I'm asking you for your help.
row_faces = 3224;
col_faces = 3;
for r = 1:row_faces;
for c = 1:col_faces;
if ??? %if the value of the element (whose index is equal to the vertex (r,c) in "faces") of the logical array is false
faces(r,c) = 0;
end
end
end
I thank you in advance.

采纳的回答

Walter Roberson
Walter Roberson 2022-1-7
to_keep = find(logical_values);
new_faces = faces(all(ismember(faces, to_keep),3),:);
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by