How to remove repeating characters in a character array?

조회 수: 7 (최근 30일)
Rui Xu
Rui Xu 2021년 11월 29일
댓글: Stephen23 2021년 11월 29일
For example, I have a 4 x 2 char array, moviecountry. It looks like this, ['US'; 'FR'; 'US'; 'CA']. I would like to have another character array, states, that only stores the non-repeating elements of this array, so in this case, states = ['US'; 'FR'; 'CA']. I have the following code right now:
states = [];
for jj = 1:size(moviecountry,1)
currentcountry = moviecountry(jj,:); %set temp country and compare with other
if jj == 1
states = [currentcountry];
end
for kk = 1:size(states,1)
if ~ismember(currentcountry, states(kk, :))
states = [states;currentcountry]%only add to output if no repetition
end
end
end

채택된 답변

Adam Danz
Adam Danz 2021년 11월 29일
See unique.
  댓글 수: 5
Adam Danz
Adam Danz 2021년 11월 29일
Note Stephen's use of column vectors. If M is a row vector you won't be able to use this method.
Stephen23
Stephen23 2021년 11월 29일
@Adam Danz: I just copied the char array verbatim from the question: "I have a 4 x 2 char array, moviecountry. It looks like this, ['US'; 'FR'; 'US'; 'CA']."

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by