How do I remove duplicates from a structure array?
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Craig
am 16 Mai 2013
Kommentiert: John King
am 5 Mai 2021
Suppose I create the following structure:
animals(1).name='Northern Mockingbird';
animals(1).class='Bird';
animals(2).name='Cheetah';
animals(2).class='mammal';
animals(3).name='Wolf';
animals(3).class='mammal';
animals(4).name='Eagle';
animals(4).class='Bird';
animals(5).name='Turtle';
animals(5).class='Reptile';
animals(6).name='Bee';
animals(6).class='Insect';
animals(7).name='Chameleon';
animals(7).class='Reptile';
animals(8).name='Barn Spider';
animals(8).class='Arachnid';
animals(9).name='Ant';
animals(9).class='Insect';
animals(10).name='Northern Cardinal';
animals(10).class='Bird';
Then I set animals(11) equal to animals(4), then I sort the whole thing through a littany of conversions/reshapings, the sortrows command and another littany of conversions/reshapings.
Is there some way to remove the duplicate entries from that array of structures?
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 16 Mai 2013
Bearbeitet: Azzi Abdelmalek
am 16 Mai 2013
a= {animals.name}
b= {animals.class}
c=cellfun(@(x,y) [x y],a', b','un',0)
[ii,ii]=unique(c,'stable')
animals=animals(ii)
2 Kommentare
Azzi Abdelmalek
am 16 Mai 2013
Bearbeitet: Azzi Abdelmalek
am 16 Mai 2013
%or
[ii,ii]=unique({animals.name},'stable')
animals=animals(ii)
Because if the name is the same, the class should be the same.
Weitere Antworten (1)
Jonathan Sullivan
am 16 Mai 2013
It's not pretty, but this should work:
isUnique = true(size(animals));
for ii = 1:length(animals)-1
for jj = ii+1:length(animals)
if isequal(animals(ii),animals(jj))
isUnique(ii) = false;
break;
end
end
end
animals(~isUnique) = [];
2 Kommentare
John King
am 5 Mai 2021
Wow, only had to schange the name from "animals" to my struct name and this worked for me! Thank you Jonathan!
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!