Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含 5 个数字的向量,我想用零填充它。我该怎么做?
A = [1 2 3 4 5].';
我希望零填充向量是这样的:
A_new = [0 0 0 0 0 1 2 3 4 5].';
另外,对于另一种情况,我想将 1、3、4 分配给矩阵W,如下所示,其他全部为零。长度W为7 W = [0 1 0 0 3 0 4]..
W
W = [0 1 0 0 3 0 4]
您可以使用以下代码
newA = [zeros(5,1); A]
关于另一个案例。你需要类似的东西
inds = [2 5 7]; elems = [1 3 4]; W = zeros(7,1); W(inds) = elems