flip the sign at zero crossing point
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
nashat sawai
el 3 de Oct. de 2020
Comentada: Vladimir Sovkov
el 3 de Oct. de 2020
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.
0 comentarios
Respuesta aceptada
Stephen23
el 3 de Oct. de 2020
>> 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 comentarios
Stephen23
el 3 de Oct. de 2020
Editada: Stephen23
el 3 de Oct. de 2020
@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
el 3 de Oct. de 2020
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.
Más respuestas (1)
Vladimir Sovkov
el 3 de Oct. de 2020
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 comentarios
Stephen23
el 3 de Oct. de 2020
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
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!