How to find first non repeated (unique) character in a string?

조회 수: 11 (최근 30일)
Sam Accura
Sam Accura 2019년 2월 14일
댓글: Sam Accura 2019년 2월 14일
I have already tried to find a Matlab function for this but doesnt seem to be one apart from 'unique' which only eliminates duplicates. Any ideas for code on how to do this?
For example, an input of 'hello' would return h or an input of 'radar' would return d.
The unique characters in 'hello' are h,e and o with h being the first in line
The unique character in 'radar' is only d and so this would also be the first
Any help would be appreciated
  댓글 수: 2
madhan ravi
madhan ravi 2019년 2월 14일
please illustrate with an example
Sam Accura
Sam Accura 2019년 2월 14일
Example added to question - thank you

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

채택된 답변

madhan ravi
madhan ravi 2019년 2월 14일
u=unique(s,'stable'); % where s is your input
Z=arrayfun(@(x)nnz(s==u(x)),1:numel(u));
Result=u(find(Z==1,1,'first'))

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 2월 14일
z = 'hello';
[a,~,c] = unique(z,'stable');
out = a(find(accumarray(c,1) == 1,1));

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by