필터 지우기
필터 지우기

How to delete duplicate words (ie., two words combine) in cell arrays?

조회 수: 2 (최근 30일)
Jothi
Jothi 2015년 6월 20일
댓글: Jothi 2015년 6월 20일
Sir,
My cellarray contain the following words,
C= {'the best', 'work fast', the best', 'come fast', 'forget', 'tension', 'forget', 'come fast')
How to remove the duplicate cells ie., the best, come fast and forget are duplicate cell arrays.
The output file is C1={'the best', 'work fast', 'come fast', 'forget', 'tension'}
How to get this output.
Thanks.

채택된 답변

cwshep
cwshep 2015년 6월 20일
Probably not the fastest way...
function C = dedupeCell(C)
s = repmat(true,length(C),1);
for i = 1:length(C)
for j = 1:i-1
if strcmp(C{i},C{j})
s(i) = false;
end
end
end
C = C(s);
end
Results in:
>> C= {'the best', 'work fast', 'the best', 'come fast', 'forget', 'tension', 'forget', 'come fast'};
>> dedupeCell(C)
ans =
'the best' 'work fast' 'come fast' 'forget' 'tension'
Please take the time to put accurate code in your question (your cell definition is missing a quote, and is closed with a parenthesis).

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by