遇到一个不同行数矩阵拼接问题,例如
A=[1 2 3 4 5 6]';
B=[1 2 3 4]';
基本想法是将行数小的矩阵补零,凑成行数一致,matlab中扩充矩阵的函数padarray
下面是合并AB矩阵的程序
l=max([length(A),length(B)]);
C=[padarray(A,[l-length(A) 0],'post') padarray(B,[l-length(B) 0],'post')]
程序运行结果
C =
1 1
2 2
3 3
4 4
5 0
6 0
附上Matlab文档中padarray函数的说明:
padarray
Pad(填充) array
Syntax
B = padarray(A, padsize)
B = padarray(A, padsize, padval)
B = padarray(A, padsize, padval, direction)
Description
B = padarray(A, padsize) pads array A with 0's (zeros). padsize is a vector of positive integers that specifies both the amount of padding to add and the dimension
along which to add it. The value of an element in th