MATLAB Concatenate matrices with unequal dimensions

后端 未结 3 1892
梦如初夏
梦如初夏 2021-01-14 07:02

Is there any easy way to concatenate matrices with unequal dimensions using zero padding?

short = [1 2 3]\';
long = [4 5 6 7]\';
desiredResult = horzcat(shor         


        
3条回答
  •  一整个雨季
    2021-01-14 07:35

    Matrices in MATLAB are automatically grown and padded with zeroes when you assign to indices outside the current bounds of the matrix. For example:

    >> short = [1 2 3]';
    >> long = [4 5 6 7]';
    >> desiredResult(1:numel(short),1) = short;  %# Add short to column 1
    >> desiredResult(1:numel(long),2) = long;    %# Add long to column 2
    >> desiredResult
    
    desiredResult =
    
         1     4
         2     5
         3     6
         0     7
    

提交回复
热议问题