Replace non-zero elements in array with output of a calculation on them
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
dan berkowitz
le 16 Jan 2019
Commenté : Pedapudi Bharath Raja Bhoopal
le 11 Oct 2020
Hi,
I have an array, A = [2 3 0 7 0 0 9].
How can I replace the non-zero elements of A with thier original value minus one?
I want to generate B = [ 1 2 0 6 0 0 8]
Any help would be appreciated! Thanks,
DB
0 commentaires
Réponse acceptée
Walter Roberson
le 16 Jan 2019
mask = A~=0;
A(mask) = A(mask) - 1;
You can code it as a single expression, but that risks evaluating the comparison twice:
A(A~=0) = A(A~=0) - 1;
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Language Fundamentals 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!