How to remove repeating characters in a character array?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Rui Xu
am 29 Nov. 2021
Kommentiert: Stephen23
am 29 Nov. 2021
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
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 29 Nov. 2021
5 Kommentare
Adam Danz
am 29 Nov. 2021
Note Stephen's use of column vectors. If M is a row vector you won't be able to use this method.
Stephen23
am 29 Nov. 2021
@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']."
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time 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!