flip the sign at zero crossing point

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

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by