See if this works for you
A= [ 2 3 6; 4 7 4; 8 7 2; 5 5 5; 1 8 8; 1 3 1; 7 8 2 ]; Dummy_row = [1 2 3]; b = diff(sort(A,2),1,2); b = sum(b == 0,2); b = b > 0; c = repmat(Dummy_row,sum(b),1); b = b' .* (1:length(b)); b = b(b > 0); newA = A; newA(b,:) = c;
gives
newA = 2 3 6 1 2 3 8 7 2 1 2 3 1 2 3 1 2 3 7 8 2
Edit
Don't need a lot of changes, try this,
Dummy_row = [1 2 3]; b = sum(A == 0,2); b = b > 0; c = repmat(Dummy_row,sum(b),1); b = b' .* (1:length(b)); b = b(b > 0); newA = A; newA(b,:) = c;
source share