Merge multiple matrices with different columns into a single matrix
Show older comments
I would like to combine several matrices with different columns (cell_test(:,1)) into one matrix by adding 0's (or rather []) in the missing values (see 'M' matrix).
cell_test = importdata("cell_test.mat"); %first column: matrices; second column: number of columns in the matrix
col = cell_test(1:3,2);
col = cell2mat(col);
col_max = max(col);
null_A = repmat(0,2,col_max-cell_test{1,2});
null_C = repmat(0,2,col_max-cell_test{3,2});
A_new = [cell_test{1,1} , null_A];
C_new = [cell_test{3,1} , null_C];
M = [A_new; cell_test{2,1}; C_new]; % result (where 0 become '[]' (possible?))
I would try to get the matrix 'M' as generically as possible, i.e. that I want to get that result even if I have multiple rows within 'cell_test'.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!