Delete duplicate values from string array

27 ビュー (過去 30 日間)
Tatjana Mü
Tatjana Mü 2022 年 5 月 17 日
回答済み: Mitch Lautigar 2022 年 5 月 17 日
Hi,
I have a string array with different elements. They are random from sample to sample. In some rows I have duplicates from some elements. Which should be remove. But sadly I don't find a correct way.
SP_RL = ["Na","Mg","Mg","Si"; "K","Ca","Rb"; "Sr","Sr","Pt"]
In the end I want to have it like this:
SP_RL = ["Na","Mg","Si"; "K","Ca","Rb";"Sr","Pt"]
I tried it already with unique in a loop to delete the duplicates row after row:
for x=1:number_rows
SP_RL(x,:)=unique(SP_RL(x,:));
end
At the moment I am out of ideas, because I tried everything. Does somebody has experience with the same problem?
Thank you :-)
  6 件のコメント
Tatjana Mü
Tatjana Mü 2022 年 5 月 17 日
@Thorsten that's bad... Ok like you mentioned I could turn it into a long vector. Does this means I have every row in a long vector or the whole matrix in one?
Torsten
Torsten 2022 年 5 月 17 日
編集済み: Torsten 2022 年 5 月 17 日
You can first delete the duplicates rowwise and afterwards put the resulting (possibly shortened) rows in a long vector.
It should also be possible to turn your matrix into a cell array. Then each row can have a different number of elements, I guess. But I'm not an expert with this kind of data structure.
You can also keep the matrix structure if you replace the duplicate strings by another string, e.g. "Deleted".

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

回答 (1 件)

Mitch Lautigar
Mitch Lautigar 2022 年 5 月 17 日
You need to use strcmpi() which will compare strings ignoring case and have it index through each spot of the array to compare the strings.
Unique() is best for sorting numbers. Hope this helps!

カテゴリ

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