MATLAB 连接具有不等维数的矩阵
有没有简单的方法可以使用零填充来连接具有不等维度的矩阵?
short = [1 2 3]';
long = [4 5 6 7]';
desiredResult = horzcat(short, long);
我想要这样的东西:
desiredResult =
1 4
2 5
3 6
0 7
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(short, long);
I would like something like:
desiredResult =
1 4
2 5
3 6
0 7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您分配给矩阵当前边界之外的索引时,MATLAB 中的矩阵会自动增长并用零填充。例如:
Matrices in MATLAB are automatically grown and padded with zeroes when you assign to indices outside the current bounds of the matrix. For example:
编辑:
我已经编辑了之前的解决方案,这样您就不必向函数提供
maxLength
参数。该函数在进行填充之前计算它。将其作为函数的便利之处在于,您可以轻松地将多个不均匀向量连接在一行中,例如 joinUnevenVectors(vec1,vec2,vec3,vec4) 等,而无需手动输入每行。
例子:
EDIT:
I have edited my earlier solution so that you won't have to supply a
maxLength
parameter to the function. The function calculates it before doing the padding.The convenience of having it as a function is that you can easily join multiple uneven vectors in a single line as
joinUnevenVectors(vec1,vec2,vec3,vec4)
and so on, without having to manually enter it in each line.EXAMPLE:
当写入矩阵中不存在的元素时,Matlab 会自动进行填充。
因此,另一种非常简单的方法如下:
Matlab automatically does padding when writing to a non-existent element of a matrix.
Therefore, another very simple way of doing this is the following: