create vector based on string data
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
その他の回答 (1 件)
Guillaume
2019 年 3 月 12 日
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!