matlab - Concatenating cell arrays of different sizes -
it appreciated if me concatenate 2 cell arrays have different sizes. example, consider cell arrays:
a={'p' 'e' 't' 'k'; 2 3 4 6; 3 5 9 8; 5 4 1 0; 8 9 6 5}; b={'a' 'v'; 1 2; 3 4; 0 5; 6 8};
array b
have different size, depending on iteration result. want combine these cell arrays, end with
c={'p' 'e' 't' 'k';2 3 4 6; 3 5 9 8; 5 4 1 0; ... 8 9 6 5;'a' 'v' nan nan;1 2 nan nan; 3 4 nan nan;0 5 nan nan; 6 8 nan nan};
how can this, when sizes of a
, b
different each time run code?
you first need "pad" smaller cell array, can concatenate both cell arrays with standard methods. in comment question indicated want pad matrix nan
. how it, assuming width of array b
smaller or equal width of array a
:
a={'p' 'e' 't' 'k';2 3 4 6; 3 5 9 8; 5 4 1 0;8 9 6 5}; b={'a' 'v' ;1 2; 3 4;0 5; 6 8}; sa = size(a); sb = size(b); columns_to_pad = sa(2) - sb(2); padding = num2cell(nan*ones(sb(1), columns_to_pad)); b_padded = [b, padding]; c = [a; b_padded];
Comments
Post a Comment