How to flip values greater than zero in a row
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tanmoyee Bhattacharya
le 16 Avr 2018
Modifié(e) : Stephen23
le 16 Avr 2018
If I have a row in matrix like that
0 0 0 0 1 2 3 0 0 0
I want to make it like that
0 0 0 0 0 0 3 2 1 0
If it is possible to do?
1 commentaire
Stephen23
le 16 Avr 2018
@Tanmoyee Bhattacharya: the numbers a not only flipped L-R, but have been translated too. How is the new position determined?
Réponse acceptée
Birdman
le 16 Avr 2018
Try this:
A=[0 0 0 0 1 2 3 0 0 0] idx=find(A~=0); A(idx+nnz(A)-1)=fliplr(A(idx)); A(idx(1:end-1))=0
1 commentaire
Tanmoyee Bhattacharya
le 16 Avr 2018
Modifié(e) : Stephen23
le 16 Avr 2018
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!