Subtract each column of matrix until -3 is finished
Afficher commentaires plus anciens
For example, i have a matrix a = [0 1 2 3 4]. How do I subtract each column by 1?
a = [0 3 2 6 4]
b = [0 2 1 2 3]
remainder = size(a, 2) - sum(b)
The remainder is -3. I need to minus from b when b(i) ~= 0 so that at the end the b = [0 1 0 1 3].
Thank you.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 14 Déc 2011
Does the minus sign of -3 indicate the third from the start or the third from the end? Your example is ambiguous about that.
Third from the start:
a(1:abs(b)) = a(1:abs(b)) - 1;
Third from the end:
a(1:end-abs(b)+1) = a(1:end-abs(b)+1) - 1;
2 commentaires
Fangjun Jiang
le 14 Déc 2011
The result are all wrong though!
Walter Roberson
le 14 Déc 2011
ind = find(a);
ind = ind(1:abs(b));
a(ind) = a(ind) - 1;
Catégories
En savoir plus sur Sparse Matrices 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!