MATLAB: How to exchange matrix rows depending on distance

matrix

hi guys, i have the following matrix A with distance D1 and matrix B with distance D2.
i want a code to exchange A rows with B rows which have higher distance, as moving row 2 in B to matrix A row 2 and remove the old row 2 in A. the output should be:
how to do this guys?

Best Answer

Hum, I'm sure I've seen a very similar question (same illustration) in the past. Is this homework?
find the index of the rows you want to exchange:
idx = find(D2 > D1);
And use that to copy the rows of B in A:
A(idx, :) = B(idx, :);
Or, using logical indexing:
A(D2>D1, :) = B(D2>D1);