# MATLAB: Comparing two cell arrays of strings of different sizes

compare multiple arraysdifferent sizeefficientismemberstrcmpstrfindstrncmp

Dear all,
I'm trying to compare two cell arrays of strings, a lot like the following:
A = [X_ABCDE;X_BCDEA;X_BCED]B = [X_A;X_B]
and the resulting array should be:
index = [1;2;2]
as I have a matrix D = [B, C] with C containing the cells I want to end up with.
I wanted to do it using a for-loop and a nested for-loop, but, as A is 8000×1 and D is 2000×2, this will take forever. I tried using strcmp and ismember, but these only work when the cells (or strings) are identical. strfind doesn't work either, because when I use strfind I need to have a for-loop over array A, and strfind only works if the smallest of both is a string.
for iCell = 1:length(A)    index = strfind(A(iCell), cell2mat(D(iCell,1)));end
A string in B is ALWAYS shorter than in A and a string in B can occur more than once in A. I hope you can help me out 🙂

A = {'X_ABCDE';'X_BCDEA';'X_BCED'}B = {'X_A';'X_B'}%X = zeros(size(A));for k = 1:numel(B)    X(strncmp(A,B{k},3)) = k;end
>> XX =   1   2   2