matlab code to make values of an array in specific ranges(index positions) equal to zero

2 ビュー (過去 30 日間)
pavan sunder
pavan sunder 2016 年 11 月 28 日
編集済み: Walter Roberson 2016 年 11 月 29 日
matlab code to make values of an array in specific ranges equal to zero
ex:
d=[1 2 3 4 5 6 7 8 9 10 11 12 13]
start_index_positions=[2 6 10]
end_index_positions=[4 8 12]
output:
d=[1 0 0 0 5 0 0 0 9 0 0 0 13]

回答 (2 件)

James Tursa
James Tursa 2016 年 11 月 28 日
for k=1:numel(start_index_positions)
d(start_index_positions(k):end_index_positions(k)) = 0;
end

Elias Gule
Elias Gule 2016 年 11 月 29 日
Hi, I hope this is want you want.
d=[1 2 3 4 5 6 7 8 9 10 11 12 13];
start_index_positions=[2 6 10];
end_index_positions=[4 8 12];
ranges = arrayfun(@(x,y) x:y,start_index_positions,end_index_positions,'UniformOutput',false);
d([ranges{:}]) = 0;

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by