Replacing row in matrix with previous row depending on condition
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
David Hill
le 19 Juin 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 commentaire
Oliver Zacho
le 19 Juin 2020
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!