flip the sign at zero crossing point

1 回表示 (過去 30 日間)
nashat sawai
nashat sawai 2020 年 10 月 3 日
コメント済み: Vladimir Sovkov 2020 年 10 月 3 日
I have an array vector (signal ) contains vales ones and zeros. e.g data set as follow
Data1 = [1,0,1,0,1,0,1,0,1,0];
I want to reverse the sign (phase) of values at zero-crossing points to become as follow
Data1=[1, 0,-1, 0, 1, 0,-1, 0, 1, 0]
do you know an elegant way to do that on a large data set?
Thanks in advance.

採用された回答

Stephen23
Stephen23 2020 年 10 月 3 日
>> Data1 = [1,0,1,0,1,0,1,0,1,0]
Data1 =
1 0 1 0 1 0 1 0 1 0
>> Data2 = Data1.*cumprod([1,diff(Data1==0)])
Data2 =
1 0 -1 0 1 0 -1 0 1 0
  4 件のコメント
Stephen23
Stephen23 2020 年 10 月 3 日
編集済み: Stephen23 2020 年 10 月 3 日
@Vladimir Sovkov: you are right, the diff can return zero. Luckily this is easily fixed:
>> Data1 = [1,0,0,1,1,0,1,0,1,1,0,0,0,1,0,0]
Data1 =
1 0 0 1 1 0 1 0 1 1 0 0 0 1 0 0
>> D = [1,diff(Data1==0)];
>> D(D==0) = 1;
>> Data2 = Data1 .* cumprod(D)
Data2 =
1 0 0 -1 -1 0 1 0 -1 -1 0 0 0 1 0 0
Vladimir Sovkov
Vladimir Sovkov 2020 年 10 月 3 日
In my understanding, the sign must alternate between every zero elements. Hence, it must change after the odd number of zeros in a sequence but must not after the even number of zeros. It is not fulfilled yet.

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

その他の回答 (1 件)

Vladimir Sovkov
Vladimir Sovkov 2020 年 10 月 3 日
Data1 = [1,0,1,1,0,1,1,0,0,1,0,1,0];
k=find(~Data1);
if ~isempty(k)
if k(end)<numel(Data1)
k(end+1)=numel(Data1)+1;
end
for n=1:2:numel(k)-1
Data1(k(n)+1:k(n+1)-1)=-1;
end
end
disp(Data1);
  2 件のコメント
Stephen23
Stephen23 2020 年 10 月 3 日
Simpler:
>> Data1 = [1,0,1,1,0,1,1,0,0,1,0,1,0]
Data1 =
1 0 1 1 0 1 1 0 0 1 0 1 0
>> Data2 = Data1 .* cumprod(1-2*(Data1==0))
Data2 =
1 0 -1 -1 0 1 1 0 0 1 0 -1 0
Vladimir Sovkov
Vladimir Sovkov 2020 年 10 月 3 日
Yes!

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by