How to set a range in a matrix to zero?

11 ビュー (過去 30 日間)
toka55
toka55 2017 年 11 月 2 日
コメント済み: KL 2018 年 1 月 24 日
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 件のコメント
Tan Chin Wee
Tan Chin Wee 2018 年 1 月 23 日
hi what does +1 and -1 means?
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]) ;
  1 件のコメント
toka55
toka55 2017 年 11 月 2 日
Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by