Zero pad a vector in MATLAB

6.4k Views Asked by At

I have a vector that contains 5 numbers and I want to pad it with zeros. How can I do it?

A = [1 2 3 4 5].';

I want the zero padded vector to be like this:

A_new = [0 0 0 0 0 1 2 3 4 5].';

Also, for another case, I want to assign 1, 3, 4 to matrix W as follows, with all else being zeros. The length of W is 7. W = [0 1 0 0 3 0 4].

1

There are 1 best solutions below

1
kvorobiev On

You can use following code

newA = [zeros(5,1); A]

About another case. You need something like

inds = [2 5 7];
elems = [1 3 4];
W = zeros(7,1);
W(inds) = elems