create vector based on string data
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Elysi Cochin
am 12 Mär. 2019
Beantwortet: Guillaume
am 12 Mär. 2019
cell_array = {
'FA.EA';
'FA.EB';
'FA.EC';
'FA.EA';
'FA.EB';
'FB.EC';
'FB.EA';
'FB.EB';
'FC.EC';
'FC.ED';
'FC.EA';
'FC.EB'}
Based on the above cell_array i wanted to create 2 vectors.
vector1 = [1 1 1 1 1 2 2 2 3 3 3 3]; % based on the first 2 characters FA FB and FC
The 3 categories can increase. It must work for any data (FA FB FC FD FE FF...)
vector2 = [1 2 3 1 2 3 1 2 3 4 1 2]; % based on the charcters EA EB EC ED
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Guillaume
am 12 Mär. 2019
I would do it like this:
cell_array = {
'FA.EA';
'FA.EB';
'FA.EC';
'FA.EA';
'FA.EB';
'FB.EC';
'FB.EA';
'FB.EB';
'FC.EC';
'FC.ED';
'FC.EA';
'FC.EB'}
splitted = regexp(cell_array, '([^.]+)\.(.+)', 'tokens', 'once'); %split at the .
splitted = vertcat(splitted{:});
[~, ~, vector1] = unique(splitted(:, 1))
[~, ~, vector2] = unique(splitted(:, 2))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!