Let's say I've got a vector a = [1 2 4]
. I want it converted into a vector which looks like this b = [1 2 0 4]
, i.e. each number is placed into a correct position and since 3
is not included into the vector a
, it is replaced by 0
in vector b
. This can be done the following way:
a = [1 2 4]
b = zeros(1, size(a, 2));
b(1, a) = a;
I can't figure out a way to do the same for a matrix. For example,
c = [1 4 2 0; 3 1 0 0; 4 0 0 0; 1 3 4 0];
I need to convert into a matrix that looks like this:
d = [1 2 0 4; 1 0 3 0; 0 0 0 4; 1 0 3 4];
Any tips? How can this be done? How can I do this without using loops?