필터 지우기
필터 지우기

How do I remove duplicates from a structure array?

조회 수: 14 (최근 30일)
Craig
Craig 2013년 5월 16일
댓글: John King 2021년 5월 5일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 16일
편집: Azzi Abdelmalek 2013년 5월 16일
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
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 16일
편집: Azzi Abdelmalek 2013년 5월 16일
%or
[ii,ii]=unique({animals.name},'stable')
animals=animals(ii)
Because if the name is the same, the class should be the same.
Craig
Craig 2013년 5월 17일
Agreed. The actual structure array is file names and their respective paths; I used the animal array because it is easier to check, the difference (that I had not thought of) is that there could be files that have identical names but reside in different folders. Since your first solution looks for unique name/class pairs (through concatenation), it will still work for file names and paths.
'c=cellfun(@(x,y) [x y],a', b','un',0)' was baffling to me. I sort of understand the 'cellfun' and am slowly getting acquainted with the '@' symbol, but the '(x,y) [x y]' part was confusing to me at first. I think I get it now though. I couldn't find the 'un' property in the documentation; is it the same as 'UniformOutput'?

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

추가 답변 (1개)

Jonathan Sullivan
Jonathan Sullivan 2013년 5월 16일
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
Craig
Craig 2013년 5월 17일
Thanks for the quick answer. I was hoping to avoid for loops, though I will admit that the loops you came up with are way prettier than I had thought a for loop solution would look like.
John King
John King 2021년 5월 5일
Wow, only had to schange the name from "animals" to my struct name and this worked for me! Thank you Jonathan!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by