I have two vectors, x and y. I would like to make third vector, z, which has the combined magnitude of x and y, i.e. sqrt(x.^2 + y.^2) but has the sign of whichever of x and y has the bigger individual magnitude. If the example below, z should be [-10.20 5.39 10.77 -10.44]. However I am incorrectly indexing the matrix bothVectorsTogether, so that it returns a 4x4 matrix instead of a 4x1 vector. What is the right way to index it? The "real" x and y have 950000 elements so I want to avoid looping through each row to identify the sign.
x = [-10 5 4 -10]';
y = [2 -2 10 -3]';
zMagnitude = sqrt(x.^2 + y.^2);
[~, whichIsBigger] = max(abs([x y])');
bothVectorsTogether = [x y];
zSign = sign(bothVectorsTogether(:, whichIsBigger));
z = zMagnitude.*zSign;