Reputation: 719
I would like to vertically concatenate multiple matrices with different dimensions: 180 x n double with n >= 7. To make these matrices have the same dimensions, I want to pad 0's as fillers. However, there is one caveat: the fillers have to go before the last 7 columns in each original, meaning that after concatenation, the last 7 columns (from the right) always stay the same. Here is an example with 3 matrices to concatenate:
R1 is 180 x 13 double
R2 is 180 x 7 double
R3 is 180 x 10 double
I want to create R_concat = 540 x 13 double
(540=180x3, 13 is the highest number of columns across the three to-be-concatenated matrices). Thus, new matrices with less than 13 columns will have to be padded to have 13 columns. As R1 has the highest number of columns, no padding is needed. For R2, 6 extra columns of zeros are needed (180x6 to be more precise). These columns will have to be added as the first 6 columns so the 7 original columns will follow. For R3, 3 extra columns of zeros are needed but these extra columns will go in between the original 3 and 4 columns. This way, the original 7 last columns (i.e., column 4 to column 10) are still the 7 last columns in the new matrix (but now they will be column 6 to 13).
I'm sorry if this explanation is somewhat clumsy. Could anyone help?
Upvotes: 0
Views: 408
Reputation: 15837
*place matrices into a cell array
*using cellfun
apply padding to each matrix
*using cell2mat
concatenate matrices
%place matrices into a cell
matrices = {rand(5,13),rand(5,7),rand(5,10)};
% column number that padding should be applied before it
pad_column = 7;
%find maxmum of number of columns of matrices
sz=cellfun('size',matrices,2);
mx = max(sz);
%pad each matrix
padded = cellfun(...
@(M)...
[...
M(:,1:end-pad_column),...
zeros(size(M,1),mx-size(M,2)),...
M(:,end-pad_column+1:end)...
],...
matrices,...
'UniformOutput', false...
);
%concatenate matrices
out = cell2mat(padded.');
Upvotes: 2