フィルターのクリア

How can I change the label for my elements with same name?

1 回表示 (過去 30 日間)
DulceEien
DulceEien 2021 年 8 月 12 日
コメント済み: DulceEien 2021 年 8 月 12 日
How can I count the the strings with the sabe value and then add them in another ID with the increasing number for the elements with the same label
For example I have the next vector
A = {'1_01';'1_01';'1_01';'1_02';'1_02'}
and the result should be this
B = {'1_01-1';'1_01-2';'1_01-3';'1_02-1';'1_02-2'}
str1 = '-';

採用された回答

Chunru
Chunru 2021 年 8 月 12 日
A = {'1_01';'1_01';'1_01';'1_02';'1_02'};
uA = unique(A);
B = A;
for i=1:length(uA)
idx = find(strcmp(A, uA(i)));
for j=1:length(idx)
B{idx(j)} = sprintf('%s-%d', B{idx(j)}, j);
end
end
%B = {'1_01-1';'1_01-2';'1_01-3';'1_02-1';'1_02-2'}
B
B = 5×1 cell array
{'1_01-1'} {'1_01-2'} {'1_01-3'} {'1_02-1'} {'1_02-2'}
  1 件のコメント
DulceEien
DulceEien 2021 年 8 月 12 日
thank you, it is working :))

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

その他の回答 (0 件)

カテゴリ

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