用不同的开始/结束元素编号重整行 [英] Reshape row wise w/ different starting/ending elements number
问题描述
问题是:
Product of known dimensions, 3, not divisible into total number of elements, 16.
这是因为我想在3x6
矩阵中reshape
一个16x1
矩阵.问题是起始矩阵有16个元素,而最终矩阵有18个元素.是否存在一种聪明的方法来按行整形并用0填充丢失的元素,直到元素数量匹配为止?
this because i want to reshape
a 16x1
matrix in a 3x6
matrix. The problem is that the start matrix has 16 elements and the final matrix has 18. Is there a smart way to reshape row wise and filling the missing element with 0s till the number of elements matches?
当然,我需要一个独立于这些数字的通用方法,因为矩阵的大小可以改变.
Of course I need a general method independent from those number since the size of matrices can change.
TBN:0应当位于矩阵的结尾
TBN: 0s should be at the end of the matrix
推荐答案
方法1
您可以使用 vec2mat
是 Communications System Toolbox 的一部分,假设A
作为输入向量-
Approach #1
You can use vec2mat
that's part of the Communications System Toolbox, assuming A
as the input vector -
ncols = 6; %// number of columns needed in the output
out = vec2mat(A,ncols)
样品运行-
>> A'
ans =
4 9 8 9 6 1 8 9 7 7 7 4 6 2 7 1
>> out
out =
4 9 8 9 6 1
8 9 7 7 7 4
6 2 7 1 0 0
方法2
如果没有该工具箱,则可以使用基本功能来实现相同功能-
Approach #2
If you don't have that toolbox, you can work with the basic functions to achieve the same -
out = zeros([ncols ceil(numel(A)/ncols)]);
out(1:numel(A)) = A;
out = out.'
这篇关于用不同的开始/结束元素编号重整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!