How to set a range in a matrix to zero?

4 visualizaciones (últimos 30 días)
toka55
toka55 el 2 de Nov. de 2017
Comentada: KL el 24 de En. de 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?

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by