Delete Negative Duplicates from Array

2 visualizzazioni (ultimi 30 giorni)
Sebastian
Sebastian il 25 Mar 2024
Commentato: Matt J il 25 Mar 2024
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!

Risposta accettata

Matt J
Matt J il 25 Mar 2024
Modificato: Matt J il 25 Mar 2024
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 Commenti
Sebastian
Sebastian il 25 Mar 2024
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.

Accedi per commentare.

Più risposte (1)

Catalytic
Catalytic il 25 Mar 2024
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

Categorie

Scopri di più su Creating and Concatenating Matrices 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