Find duplicated values in a row and rename

15 ビュー (過去 30 日間)
Manuel
Manuel 2013 年 7 月 16 日
回答済み: Curtis N 2015 年 1 月 26 日
I have a data array (100:500), and for the first vector(1,:) I would like to find the duplicated string values(the data in each cell is an string). For the result duplicated stings found I want to concatenate '_2'...'_n' depending on the duplicates found. I have try with the unique and histc functions but since is a vector of strings I could not achieve a good result.
I would really appreciate some help,
Thanks in advance

採用された回答

Curtis N
Curtis N 2015 年 1 月 26 日
I'm sure you no longer need this, but as I was looking for this type of solution today, here's how I did it using A K's suggestion.
orig = {'a', 'a', 'b', 'c', 'd', 'b', 'a', 'b', 'a', 'e'}';
new = orig;
uniqStr = unique(orig);
for ii = 1:length(uniqStr)
idx = strmatch(uniqStr{ii}, orig, 'exact');
for jj = 2:length(idx)
new{idx(jj)} = [orig{idx(jj)}, '_', num2str(jj)];
end
end
compare = [orig, new]
compare =
'a' 'a'
'a' 'a_2'
'b' 'b'
'c' 'c'
'd' 'd'
'b' 'b_2'
'a' 'a_3'
'b' 'b_3'
'a' 'a_4'
'e' 'e'
If somebody knows how to due this without loops, please share the solution.

その他の回答 (1 件)

A K
A K 2013 年 7 月 16 日
You can try using "unique" function to get a listing of all unique strings in your row vector
Next try using "strmatch" for each uniqe string found to see if they are repeated. You can try
index = strmatch('unique_string', array_of_strings, 'exact'), where index will point to the locations in array_of_strings where unique_string occurs.
  1 件のコメント
Manuel
Manuel 2013 年 7 月 17 日
I have tried but does not work or I did not understund how should work, in raw(1,:) there are two duplicated strings and does not detect the match between the others.
index = strmatch(unique(raw(1,:)), raw(1,:), 'exact')
Result:
index = []
Any more idea? thank you beforehand,

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by