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 件のコメント
Sam Accura
Sam Accura 2019 年 2 月 14 日
Thank you very much - very helpful.

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangePulsed Waveforms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by