How to set a range in a matrix to zero?

55 Ansichten (letzte 30 Tage)
toka55
toka55 am 2 Nov. 2017
Kommentiert: KL am 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?

Akzeptierte Antwort

KL
KL am 2 Nov. 2017
A([1:3-1 5+1:end]) = 0
  3 Kommentare
KL
KL am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 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