Replacing row in matrix with previous row depending on condition
Show older comments
Hi
Say I have a matrix:
A=[2,5,3;5,8,2;1,-2,5]
If a value in any of the rows are -2 the whole row should be replaced by the previous row.
So the result would be:
A=[2,5,3;5,8,2;5,8,1]
The matrix consists of 1 million rows, so I'm looking for the fastest method.
Accepted Answer
More Answers (1)
David Hill
on 19 Jun 2020
If you have consecutive rows containing -2, you will have to repeat until all the -2 rows have been replaced if that is your goal
[a,~]=find(A==-2);
a=unique(a);
A(a,:)=A(a-1,:);
1 Comment
Oliver Zacho
on 19 Jun 2020
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!