MATLAB: Compare the number of elements in each row of 2 cell arrays (A and B) and determine which array contains minimum number of elements for that row and save them.

compare cell arrays

I have 2 cell arrays A and B of dimensions 7919×255. These of course have empty and non empty elements in each row. I need to compare each row of A and B (excluding the non empty elements) and find out which row among the 2 cell arrays has the least elements and save it. I have attached sample images of how my arrays look like. The first image is a portion of A and the second a portion of B.
So for example, I need to compare row 1 of A and B and say A has 1 non empty and B has 7 non empty, so number of elements in row 1 for A<B. I need to do this for each row and save it. Any help would be much appreciated, thanks a ton! I have tried the min function but it doesn't accept inpus of cell type arrays.

Best Answer

Based on the explanation in your comment:
isA = ~cellfun('isempty',A);
isB = ~cellfun('isempty',B);
csA = cumsum(isA,2);
csB = cumsum(isB,2);
mxA = max(csA,[],2);
mxB = max(csB,[],2);
or4 = xor(mxA<4,mxB<4);
R = numel(or4);
C = cell(R,8);
for k = 1:R
if or4(k)
% "either A or B individually have less than 4 non-empty elements"
V = [A(k,isA(k,:)),...
B(k,isB(k,:))];
else % "A and B individually have more than 4 non-empty elements"
% and also "both A and B individually have less than 4 elements"
V = [A(k,isA(k,:)&csA(k,:)<=4),...
B(k,isB(k,:)&csB(k,:)<=4)];
end
idx = 1:min(size(C,2),numel(V));
C(k,idx) = V(idx);
end
And checking the output:
>> C{3,:} % "Row 3 in C will be first 4 elements of A then first 4 elements of B"
ans = ACTGCGTCAAATGGTCTGTCGGTCG
ans = ATATGACTGAGACAATGACTAGAAG
ans = ACTCCGGTGGAGTCTCTCCAGCACA
ans = CGAGGTCTCAAACGCGCTGGAAGAA
ans = TATGCACACTTCTAGTCATTGTCTC
ans = TACACACCACCCATGATAAGGTCAG
ans = ATTTTGTACACACCACCCATGATAA
ans = AAGTGTGCTTGAGTCTGTATTTTGA
>> C{1,:} % "Row 1 in C will have 1 element from A and the remaining 7 from B"
ans = CAAAAAGGTGCAGTAGGAGATATTG
ans = TTAGCCGCCGGGCTTCTGGTGTTAA
ans = GATTCTTCTGTGTGTTGTTGACGTG
ans = CTTTGGATTCTTCTGTGTGTTGTTG
ans = GAAATCTCTTATTTTGCTTTGGATT
ans = GCGTCGTTCTAGTAAGAAGTGGCTG
ans = CAAGTAAGCCTCAGCCCTCTGAGGT
ans = GTCCCTAAGAGTGTTACTGTGTAGT
>> C{6,:} % "Row 6 has 2 non-empty elements in A and two non-empty elements in B. C will have 4 non-empty elements and 4 empty elements"
ans = AAACCAGCCAGCCATGTGGGTTGTA
ans = ATTCCTCCGGCAGCTATCATAGCAA
ans = AAAACGATCGGCCGCTCGAGCGACC
ans = TTCTGCCAAGAGCCCCTTCCCAATG
ans = []
ans = []
ans = []
ans = []