How to set a range in a matrix to zero?

118 views (last 30 days)
toka55
toka55 on 2 Nov 2017
Commented: KL on 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?

Accepted Answer

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

Sign in to comment.

More Answers (1)

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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!