遇到一个不同行数矩阵拼接问题,例如
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 the vector specifies the amount of padding to add. The order of the element in the vector specifies the dimension along which to add the padding.( padsize中第一个元素为填充的行数,第二个元素填充的列数)
For example, a padsize value of [2 3] means add 2 elements of padding along the first dimension(第一维,即行) and 3 elements of padding along the second dimension(第二维,即列). By default, paddarray adds padding before the first element and after the last element along the specified dimension.
B = padarray(A, padsize, padval) pads array A where padval specifies the value to use as the pad value(填充值,若不填则默认为0). padarray uses the value 0 (zero) as the default. padval can be a scalar that specifies the pad value directly or one of the following text strings that specifies the method padarray uses to determine the values of the elements added as padding.
Value | Meaning |
'circular' | Pad with circular repetition of elements within the dimension(重复元素). |
'replicate' | Pad by repeating border elements of array(边界元素). |
'symmetric' | Pad array with mirror reflections of itself(镜像元素). |
B = padarray(A, padsize, padval, direction) pads A in the direction specified by the string direction. direction can be one of the following strings. The default value is enclosed in braces ({}).
Value | Meaning |
{'both'} | Pads before the first element and after the last array element along each dimension. This is the default(默认填充于元素前和元素后). |
'post' | Pad after the last array element along each dimension(填充于元素后). |
'pre' | Pad before the first array element along each dimension(填充与元素前). |
Class Support
When padding with a constant value, A can be numeric or logical. When padding using the 'circular', 'replicate', or 'symmetric' methods, A can be of any class. B is of the same class as A.
转载自:http://shanyunh.blog.163.com/blog/static/788787201142544537215/