MATLAB: Overwriting certain lines in a matrix with previous lines that satisfy a condition.

matrixoverwrite datavector

Hi.
I have a vector which marks rows that needs to be overwritten in a matrix.
The marked rows should be replaced with the previously row that isn't marked.
Line 1 will never be marked so no need to take account for the potential error.
The matrix is big and will always have enough rows that markedrows refers to.
Ex:
Vector: markedrows = [3,6,7,14,15,16,17] (markedrows will always be in ascending order)
So the deal is, according to markedrows, in the matrix:
  • The new line 3 should be the same as line 2
  • The new line 6 should be the same as line 5
  • The new line 7, however, shouldn't be line 6 cause line 6 is marked too, it should instead be line 5.
Following this pattern would make line 14, 15, 16 and 17 all turn into line 13.
I hope this makes sence and that you can help :).

Best Answer

Simply do:
for row = markedrows
m(row, :) = m(row-1, :);
end
row 14 is replaced by row 13, row 15 by row 14 which is now equal to row 13, etc.