How to set a range in a matrix to zero?

7 vues (au cours des 30 derniers jours)
toka55
toka55 le 2 Nov 2017
Commenté : KL le 24 Jan 2018
I have a matrix like:
A=[2 4 8 1 4 6]
I want to set everything to zero accept element 3-5.
The result should be
A=[0 0 8 1 4 0]
How can I do that?

Réponse acceptée

KL
KL le 2 Nov 2017
A([1:3-1 5+1:end]) = 0
  3 commentaires
Tan Chin Wee
Tan Chin Wee le 23 Jan 2018
hi what does +1 and -1 means?
KL
KL le 24 Jan 2018
the question is to set everything to zero accept element 3-5. So +1 and -1 captures the elements beyond these limits. In this example, 1 to 2 and 6 to the last element.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 2 Nov 2017
A=[2 4 8 1 4 6] ;
B = zeros(size(A)) ;
B([3,5]) = A([3,5]) ;

Catégories

En savoir plus sur Creating and Concatenating Matrices 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!

Translated by