Search code examples
stringmatlabconcatenationstring-concatenationcell-array

meshgrid equivalent for strings


I have two cells:

Months1 = {'F','G','H','J','K','M','N','Q','U','V','X','Z'};
Months2 = 2009:2014;

How do I generate all combinations without running a loop so that I achieve the following:

Combined = {'F09','F10','F11','',...,'G09',.....};

Basically all combinations of Months1 and Months2 as in meshgrid.


Solution

  • You can convert cell array to indices with grp2idx, then use meshgrid, then strcat to combine strings. Before you also need to convert numeric Months2 vector to cell array of strings.

    [id1,id2] = meshgrid(grp2idx(Months1),Months2);
    Months2cell = cellstr(num2str(id2(:)-2000,'%02d'))';
    Combined = strcat( Months1(id1(:)), Months2cell );