How to set a range in a matrix to zero?

32 visualizzazioni (ultimi 30 giorni)
toka55
toka55 il 2 Nov 2017
Commentato: KL il 24 Gen 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?

Risposta accettata

KL
KL il 2 Nov 2017
A([1:3-1 5+1:end]) = 0
  3 Commenti
Tan Chin Wee
Tan Chin Wee il 23 Gen 2018
hi what does +1 and -1 means?
KL
KL il 24 Gen 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.

Accedi per commentare.

Più risposte (1)

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by