Delete Negative Duplicates from Array

조회 수: 2 (최근 30일)
Sebastian
Sebastian 2024년 3월 25일
댓글: Matt J 2024년 3월 25일
I have a 3-by-n array of points representing the vertices of a polyhedron. I need to identify all the axes that pass through the origin and a vertex, so I need to identify and remove all the points x where -x also exists in the array. I can manage this by iterating through every point and finding negatives, but it feels like there should exist a sneaky way to use the unique function to do the same thing but better.
Thanks in advance!

채택된 답변

Matt J
Matt J 2024년 3월 25일
편집: Matt J 2024년 3월 25일
X=randi(9,3,4); X=[X,-X(:,1:2)]
X = 3x6
5 8 7 6 -5 -8 4 8 9 3 -4 -8 2 5 4 1 -2 -5
map=triu(squeeze(~any(X+reshape(X,3,1,[]),1)));
[I,J]=find(map);
X(:,[I;J])=[]
X = 3x2
7 6 9 3 4 1
  댓글 수: 2
Sebastian
Sebastian 2024년 3월 25일
That's pretty impressive, but unfortunately I want to only get rid of 1 of the pair of opposite vectors, not both. For instance, in your example, I'd want it to only remove the last two columns (or just the first two). Sorry if that wasn't clear.
Matt J
Matt J 2024년 3월 25일

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Catalytic
Catalytic 2024년 3월 25일
X=rand(3,4); X=[X,-X(:,1)]
X = 3x5
0.2765 0.5016 0.7075 0.8914 -0.2765 0.8997 0.8842 0.9797 0.0281 -0.8997 0.7290 0.1868 0.9017 0.4237 -0.7290
[~,~,G]=unique([X,-X]','rows');;
[N,~,bin]=histcounts(G,1:max(G)+1);
bin=bin(1:end/2);
X(:,N(bin)>1)=[]
X = 3x3
0.5016 0.7075 0.8914 0.8842 0.9797 0.0281 0.1868 0.9017 0.4237

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by