MATLAB: Subtract corresponding nonzero element from each element in a vector

matrix manipulation

Ok, I want to create an M x M matrix A with the following.
Vec1=[0 1 1 0] Vec2=[1 3 5 7]
Then, matrix A has size M = size(Vec1) = size(Vec2) and all elements start at some constant value c.
I want to update the matrix A such that, for each index where Vec1 is not zero, create a matrix row where the row values are obtained by subtracting the value of Vec2 at that same index from each other value and getting the absolute values.
So in the above example, if I initialize matrix A as:
c c c c
c c c c
c c c c
c c c c
then, the first row stays the same (Vec 1 is zero) the second row becomes [abs(3-1), abs(3-3), abs(3-5), abs(3-7)], Third row becomes [abs(5-1), abs(5-3), abs(5-5), abs(5-7)]. Fourth row stays the same, so we end up with:
c c c c
2 0 2 4
4 2 0 2
c c c c
I would like to accomplish this without loops or possibly without even repmats and the like, since the matrices and vectors in question are huge.
Thanks!

Best Answer

%EDIT again
Vec1=[0 1 1 0];
Vec1(~Vec1) = nan;
Vec2=[1 3 5 7];
C2 = bsxfun(@times,Vec1.',abs(bsxfun(@minus,Vec2.',Vec2)));
C2(isnan(C2)) = A(isnan(C2));