MATLAB: Join 2 numeric cell arrays using a common key

cell arryjoin;keymerge

Hi all, I need to join 2 cell arrays. One is shorter than the other. The resulting cell array should (or matrix…anything I can plot) have the length of the shorter array. They have conceptually this structure:
DATA_A =
[1] [1111]
[2] [2222]
[3] [3333]
[4] [4444]
[5] [5555]
DATA_B =
[1] [2.1]
[4] [2.2]
[5] [2.3]
what I want:
DATA_C =
[1111] [2.1]
[4444] [2.2]
[5555] [2.2]
so the first column should be used as a key to combine both arrays. Array A contains all integers, while Array B has "gaps". hope its clear and thanks a lot!

Best Answer

DATA_A = {[1], [1111]; ...
[2], [2222]; ...
[3], [3333]; ...
[4], [4444]; ...
[5], [5555]};
DATA_B = {[1], [2.1]; ...
[4], [2.2]; ...
[5], [2.3]};
keyA = cat(2, DATA_A{:, 1});
keyB = cat(2, DATA_B{:, 1});
[iB, iA] = ismember(keyB, keyA);
DATA_C = cat(2, DATA_A(iA, 2), DATA_B(iB, 2));
I cannot guess what "anything I can plot" exactly mean. Perhaps you want a cell2mat or:
DATA_C = [cat(1, DATA_A{iA, 2}), cat(1, DATA_B{iB, 2})];