How to set a range in a matrix to zero?

29 次查看(过去 30 天)
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?

采纳的回答

KL
KL 2017-11-2
A([1:3-1 5+1:end]) = 0
  3 个评论
KL
KL 2018-1-24
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.

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by