フィルターのクリア

How to Convert vector elements to zero for certain N length when its values gets negative?

1 回表示 (過去 30 日間)
I have a column vector F= (0; 0; 0; 0; 5; 7; 12; 11; 23; 32; 22; 10; 8; -6 0; -8; 1; 4; 8; -5; 0; 0; 0; 6; 8; 9; 13; 14; 12; 23; 34; 22; 16; 17; 4; -5 ; -6; 5; 7; 0; 0; 0)
I want to convert values of F when it gets negative (in this case -6 and -5) till N (lets N=8)
so result would be F= (0; 0; 0; 0; 5; 7; 12; 11; 23; 32; 22; 10; 8; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 6; 8; 9; 13; 14; 12; 23; 34; 22; 16; 17; 4; 0; 0; 0; 0; 0; 0; 0) how to do this?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 6 日
F(F<0 & F>=-8)=0
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 6 日
編集済み: Azzi Abdelmalek 2014 年 8 月 6 日
F= [0; 0; 0; 0; 5; 7; 12; 11; 23; 32; 22; 10; 8; -6 ;0; -8; 1; 4; 8; -5; 0; 0; 0; 6; 8; 9; 13; 14; 12; 23; 34; 22; 16; 17; 4; -5 ; -6; 5; 7; 0; 0; 0]
idx=find(F<0,1)
n=numel(F);
while ~isempty(idx)
F(idx:min(idx+7,n))=0;
idx=find(F'<0,1);
end
Sagar Dhage
Sagar Dhage 2014 年 8 月 7 日
Thanks Azzi...its working

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

その他の回答 (3 件)

Yu Jiang
Yu Jiang 2014 年 8 月 6 日
編集済み: Yu Jiang 2014 年 8 月 6 日
while ~isempty(find(F<0))
id1 = find(F<0,1);
id2 = min(id1 + 7, length(F));
F(id1:id2) = 0;
end
  2 件のコメント
Sagar Dhage
Sagar Dhage 2014 年 8 月 6 日
i want that 0 from this value till next 8 values....
Yu Jiang
Yu Jiang 2014 年 8 月 6 日
How about this?
while ~isempty(find(F<0))
id1 = find(F<0,1);
id2 = min(id1 + 7, length(F));
F(id1:id2) = 0;
end

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


Roger Stafford
Roger Stafford 2014 年 8 月 6 日
I am not sure what you mean in your remark "till N (lets N=8)". Do you mean that a maximum of N successive negative values in F are to be converted to 0 with all the possible remaining negative values, if any, unaffected? If so, the example you should have used ought to have demonstrated that behavior. In any case, here is code that would accomplish such a task:
F(find(F<0,N)) = 0;
  1 件のコメント
Sagar Dhage
Sagar Dhage 2014 年 8 月 6 日
N is next 8 values from -6 and -5. I want to convert all positive and negative values to 0 for this N=8 length. that is all values in old should be zero. your formula converts only negative.

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


Roger Stafford
Roger Stafford 2014 年 8 月 6 日
I think I understand you now. How about this:
n = length(F);
for k = 1:n
if F(k) < 0
F(k:min(k+N-1,n)) = 0;
end
end

カテゴリ

Help Center および File ExchangeReport Generator Utilities についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by