How can i shorten the duration this code takes? it takes 0.4 seconds to complete if you insert a longer string. I would prefer not using the for loop but it is still welcome

1 vue (au cours des 30 derniers jours)
this code scans a string for letters and ignores repeating letters, for example scans 'xyxzyx' and returns 'xyz' as there are only 3 letters present. the goal is to re-write in a way that takes less time to execute. thanks in advance... here is the code
str='shdydjsgdnsladhdksjkhdjksa'
fixed2=str; %ignore the redundancy
A1=fixed2;
for i=1:numel(fixed2)
k=fixed2(i);
A1=regexprep(A1,'(?<=(??@k).*)(??@k)','')
end

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 1 Sep 2016
what about A1=unique(str)?
  1 commentaire
Waboraro Olefile
Waboraro Olefile le 1 Sep 2016
Thanks Fangjun Jiang! i wasnt aware of the function. you just saved me a great deal of execution time!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by