MATLAB: Group the array with similar strings

arraycharstring

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?

Best Answer

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))