MATLAB: Separation of similar strings in cell array and store in different variables

separate duplicate rows

I have a cell array as data 1. I need to separate the rows containing 'aa' and save it in one variable (var1) similarly for 'bb','cc' and 'dd'.
a = {'aa';'bb';'cc';'dd';'aa';'bb';'dd';'cc';'aa'}
b = {'1';'2';'3';'4';'5';'6';'7';'8';'9'}
data1 =[a,b]
My output looks like below
Please help me if anyone knows

Best Answer

  • One way:
    [~, ~, subs] = unique(data1(:, 1));
    var = accumarray(subs, (1:size(data1, 1)).', [], @(rows) {data1(rows, :)})