Delete duplicate values from string array

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 Commenti

SP_RL = ["Na","Mg","Mg","Si"; "K","Ca","Rb"; "Sr","Sr","Pt"]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Check the input, you cannot have such a shown input.
Torsten
Torsten il 17 Mag 2022
Modificato: Torsten il 17 Mag 2022
Matrices must have a fixed number of rows and a fixed number of columns.
That's true for your original matrix SP_RL.
But if you remove elements, you get an object that is no longer a matrix.
So you will have to decide how to replace the duplicate elements by another string.
Or you turn the matrix into a long vector and then remove the duplicate elements - this will work.
Tatjana Mü
Tatjana Mü il 17 Mag 2022
Modificato: Tatjana Mü il 17 Mag 2022
@Torsten and @KSSV I added for you my original file. I import this file as a string array. And now I want to remove the replicate of the elements in the rows. It is really important that the rows are not changed, because every row is one particle.
Did you read my comment ? Removing the duplicates and keeping the matrix structure without substituting the deleted strings by another string will not work.
@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 il 17 Mag 2022
Modificato: Torsten il 17 Mag 2022
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".

Accedi per commentare.

Risposte (1)

Mitch Lautigar
Mitch Lautigar il 17 Mag 2022
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!

Categorie

Richiesto:

il 17 Mag 2022

Risposto:

il 17 Mag 2022

Community Treasure Hunt

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

Start Hunting!

Translated by