Subtract each column of matrix until -3 is finished
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 14 de Dic. de 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 comentarios
Fangjun Jiang
el 14 de Dic. de 2011
The result are all wrong though!
Walter Roberson
el 14 de Dic. de 2011
ind = find(a);
ind = ind(1:abs(b));
a(ind) = a(ind) - 1;
Categorías
Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!