Group the array with similar strings
7 次查看(过去 30 天)
显示 更早的评论
How can I find the strings that have similar string in them:
A=['x_B1_0_s_0' , 'x_B1_0_s_1', 'x_B1_0_s_2', ' y_B1_2_s_0' , 'y_B1_2_s_1', ' y_B1_2_s_2' ,' x_B2_0_s_0' ,
'x_B2_0_s_1' ,' x_B2_0_s_2' , 'y_B2_2_s_0' , 'y_B2_2_s_1' , 'y_B2_2_s_2' ]
I need to group above array to have 4 groups of strings like this:
B=['x_B1_0', 'y_B1_2', 'x_B2_0', 'y_B2_2']
Any idea how to do so?
0 个评论
采纳的回答
Guillaume
2019-11-23
It's very unclear what your definition of similar is.
Also note that your example A is a char vector, not a string, and that concatenating char vectors makes just one big char vector.
Assuming you meant your A to be a string array then one way to obtain your desired result is with:
A = ["x_B1_0_s_0" , "x_B1_0_s_1", "x_B1_0_s_2", "y_B1_2_s_0" , "y_B1_2_s_1", "y_B1_2_s_2" ,"x_B2_0_s_0" , ...
"x_B2_0_s_1" ,"x_B2_0_s_2" , "y_B2_2_s_0" , "y_B2_2_s_1" , "y_B2_2_s_2" ]
B = unique(extractBefore(A, 7))
0 个评论
更多回答(1 个)
Max Murphy
2019-11-24
Seems like you are trying to parse out a bunch of variable names and associated values and group them in a table. Ideally, it would work something like this:
% I assume it should be a cell array of char, or an array of strings
A={'x_B1_0_s_0' , 'x_B1_0_s_1', 'x_B1_0_s_2', ... % formatted
'y_B1_2_s_0' , 'y_B1_2_s_1', 'y_B1_2_s_2' , ...
'x_B2_0_s_0' , 'x_B2_0_s_1' , 'x_B2_0_s_2' , ...
'y_B2_2_s_0' , 'y_B2_2_s_1' , 'y_B2_2_s_2' };
% varExp can be changed, but does the parsing using regexp basically
varExp = '(?<label>[xy]_B\d_\d)_(?<data>\w)';
S = dynamicVarParser(A,varExp);
T = struct2table(S);
And here is the helper function I wrote:
function S = dynamicVarParser(A,varExp)
%% DYNAMICVARPARSER Parse cell array of char vectors into struct fields
%
% S = dynamicVarParser(A,nLevels);
%
% e.g.
% S = dynamicVarParser({'x_B1_0_s_0','x_B2_0_s_2'});
%%
if nargin < 2
varExp = '(?<coord>[xy])_(?<group>\w+)_(?<time>\d+)_[s]_(?<radius>\d)';
end
S = regexp(A{1},varExp,'names');
for i = 2:numel(A)
S(i) = regexp(A{i},varExp,'names');
end
end
Note that if you call it without varExp it has a default expression that is different from the one I coded above; that reflects a possible different grouping that may be useful to you based on this question, not sure what your data looks like at the end of the day.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!